拦截器中获取restful风格参数
controller层:
@GetMapping(value = "/api/users/{device}") public ResultDO get(@PathVariable("device") String device){ *** }
拦截器中:
public class ParameterInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //传统方式获取 String device = request.getParameter("device"); System.out.println("传统方式获取device: \t" + device); //map方式 Map map = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); System.out.println("map封装格式: \t" + map); return true; } *** }
配置拦截器:
@Configuration public class MyWebMvcConfig implements WebMvcConfigurer { @Autowired private ParameterInterceptor parameterInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(parameterInterceptor).addPathPatterns("/api/users/**"); } @Bean public ParameterInterceptor parameterInterceptor(){ return new ParameterInterceptor(); } }
运行sprinboot项目访问访问 URL: http://127.0.0.1:8080/api/users/andriod 测试,查看控制台输出:
传统方式获取device: null map封装格式: {device=andriod}
说明request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)这种方式能获取到restful风格的参数
相关推荐
Eiceblue 2020-08-02
ahnjwj 2020-07-28
playis 2020-06-28
playis 2020-06-16
ahnjwj 2020-06-12
84560296 2020-06-10
84560296 2020-06-09
84560296 2020-06-08
84560296 2020-05-30
81901836 2020-05-26
beibeijia 2020-05-16
85291545 2020-05-01
84560296 2020-04-10
fanix 2020-04-09
bapinggaitianli 2020-04-07
84560296 2020-03-27
85291545 2020-03-26
82911731 2020-03-25