如何在攔截器中獲取url路徑里面@PathVariable的參數(shù)值
在攔截器中獲取url路徑里@PathVariable參數(shù)值
解決辦法
Map pathVariables = (Map) request.getAttribute(
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
String classId = (String)pathVariables.get("classId");
示例接口
// 獲取某個(gè)班級(jí)下面的學(xué)生列表
@RequestMapping("/classes/{classId}/students")
public String list(@PathVariable String classId){
return "學(xué)生列表";
}
完整示例
package com.example.demo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
public class SpringMVCInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
String chatbotId = (String)pathVariables.get("classId");
System.out.println("classId: " + classId);
if ("1234".equals(classId))
return true;
return false;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class MvcInterceptorConfig extends WebMvcConfigurationSupport{
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SpringMVCInterceptor()).addPathPatterns("/chatbot/**");
super.addInterceptors(registry);
}
}
spring @PathVariable:請(qǐng)求路徑url 上有變量值,通過@PathVariable獲取
請(qǐng)求路徑url 上有個(gè)變量值,可以通過@PathVariable來獲取
示例:
id為變量值:
@RequestMapping(value = "/page/{id}", method = RequestMethod.GET)
Tool tool = new Tool();
tool.setUrls(new String[]{"finance/account/loanDownExcel/cur","finance/account/loanDownExcel/all"});
@RequestMapping("accountlog/loanDownExcel/{type}")
public void exportAccountLogExcel(@PathVariable("type") String type)throws Exception {}
或者是
public void exportAccountLogExcel(@PathVariable String type)throws Exception {}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
RabbitMQ消息隊(duì)列的目錄結(jié)構(gòu)
這篇文章主要介紹了RabbitMQ消息隊(duì)列的目錄結(jié)構(gòu),RabbitMQ?屬于消息中間件,主要用于組件之間的解耦,消息的發(fā)送者無需知道消息使用者的存在,反之亦然,那么用了那么久RabbitMQ,其目錄結(jié)構(gòu)是怎樣的呢,讓我們一起來看一下吧2023-08-08
java數(shù)據(jù)庫(kù)數(shù)據(jù)分批讀取的實(shí)現(xiàn)示例
在處理大量數(shù)據(jù)時(shí),直接從數(shù)據(jù)庫(kù)一次性讀取所有數(shù)據(jù)可能會(huì)導(dǎo)致內(nèi)存溢出或者性能下降,本文就來介紹一下java數(shù)據(jù)庫(kù)數(shù)據(jù)分批讀取的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-01-01
修改idea的這些啟動(dòng)參數(shù),令你的idea健步如飛
這篇文章主要介紹了修改idea的這些啟動(dòng)參數(shù),令你的idea健步如飛~具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
java異步調(diào)用的4種實(shí)現(xiàn)方法
日常開發(fā)中,會(huì)經(jīng)常遇到說,前臺(tái)調(diào)服務(wù),本文主要介紹了java異步調(diào)用的4種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Java設(shè)計(jì)模式之命令模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
命令模式就是對(duì)命令的封裝,下文中給大家介紹了命令模式類圖中的基本結(jié)構(gòu),對(duì)java設(shè)計(jì)模式之命令模式相關(guān)知識(shí)感興趣的朋友一起看看吧2017-08-08
使用MUI框架構(gòu)建App請(qǐng)求http接口實(shí)例代碼
下面小編就為大家分享一篇使用MUI框架構(gòu)建App請(qǐng)求http接口實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
SpringBoot可視化接口開發(fā)工具magic-api的簡(jiǎn)單使用教程
作為Java后端開發(fā),平時(shí)開發(fā)API接口的時(shí)候經(jīng)常需要定義Controller、Service、Dao、Mapper、XML、VO等Java對(duì)象。有沒有什么辦法可以讓我們不寫這些代碼,直接操作數(shù)據(jù)庫(kù)生成API接口呢?今天給大家推薦一款工具magic-api,來幫我們實(shí)現(xiàn)這個(gè)小目標(biāo)!2021-06-06

