spring boot @PathVariable傳遞帶反斜杠參數 / 的處理
我就廢話不多說了,大家還是看完整的代碼吧~
@RequestMapping(value = '/modules/{moduleBaseName}/**', method = RequestMethod.GET) @ResponseBody public String moduleStrings(@PathVariable String moduleBaseName, HttpServletRequest request) { final String path = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString(); final String bestMatchingPattern = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString(); String arguments = new AntPathMatcher().extractPathWithinPattern(bestMatchingPattern, path); String moduleName; if (null != arguments && !arguments.isEmpty()) { moduleName = moduleBaseName + ’/’ + arguments; } else { moduleName = moduleBaseName; } return 'module name is: ' + moduleName; }
補充:springboot的PathVariable接收參數值帶點號問題
問題@RequestMapping(value = '/{version}',method = RequestMethod.GET) public String demo(@PathVariable String version){ return version; }
如果version是1.0.0,則返回1.0,這儼然不是我們所期望的。
解決@RequestMapping(value = '/{version:.+}',method = RequestMethod.GET) public String demo(@PathVariable String version){ return version; }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章:
1. JSP 中response.setContentType()的作用及參數2. idea開啟代碼提示功能的方法步驟3. ASP.NET MVC使用jQuery的Load方法加載靜態頁面及注意事項4. Docker究竟是什么 為什么這么流行 它的優點和缺陷有哪些?5. ASP.NET MVC實現城市或車型三級聯動6. Springboot集成jsp及部署服務器實現原理7. SpringMVC注解之@ResponseBody注解原理8. IntelliJ IDEA 2020常用配置設置大全(方便干活)9. AJAX POST數據中有特殊符號(轉義字符)導致數據丟失的解決方法10. vue自動添加瀏覽器兼容前后綴操作
