@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