@PathVariable传递参数报错404
代码:
1
2
3
4
5
6
|
@RequestMapping ( "/test1/{a}/{b}" ) public String test1( @PathVariable int a, @PathVariable int b, Model model){ int res=a+b; model.addAttribute( "msg" ,res); return "test" ; } |
报错:
错误原因:视图解析器配置配置中,前缀少写了一个 "/" .
正确:
1
2
3
4
5
6
7
8
|
<!--视图解析器--> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" id = "internalResourceViewResolver" > <!--前缀--> < property name = "prefix" value = "/WEB-INF/jsp/" /> <!--后缀--> < property name = "suffix" value = ".jsp" /> </ bean > |
restFul风格传参, 参数中带斜杠/问题
今天遇到一个restful接口路径传参问题,我的接口路径传参带斜杠,这样和restful地址就不一致了报404错误,然后看到这样一个解决方法,亲测可用。
1
2
3
4
5
6
7
|
@GetMapping ( "user/find/by/{name}/**" ) public String getMapping( @PathVariable String name, HttpServletRequest request){ String path = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString(); String path2 = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString(); String args = new AntPathMatcher().extractPathWithinPattern(path2, path); return name + "/" + args; } |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/queen00000/article/details/105137055