spring mvc中@PathVariable / 帶斜杠方式獲取
spring mvc @PathVariable / 帶斜杠方式獲取
遇上這個(gè)問(wèn)題,百度google了一下,抄襲里面的內(nèi)容,可以實(shí)現(xiàn),在此備忘
實(shí)例
@RequestMapping(value = "/download/{value1}/**", method = RequestMethod.GET) public void getValue(@PathVariable String value1, HttpServletRequest request) throws CommonException { String value = extractPathFromPattern(request); }
private String extractPathFromPattern(final HttpServletRequest request) { String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path); }
springMVC @PathVariable中間帶/問(wèn)題處理
問(wèn)題
請(qǐng)求地址/username/resourceUrl/methodName,其中username可能有也可能沒(méi)有,resourceUrl中會(huì)帶/,這個(gè)時(shí)候要使用@PathVariable,不能正確匹配controller
解決思路
把resourceUrl處理成一個(gè)不帶/的參數(shù)即可
1、約定好/替換方案,比如請(qǐng)求方把/全部替換為--
2、通過(guò)url編碼解碼處理 / 經(jīng)過(guò)編碼變成%2F 把resourceUrl編碼后,這個(gè)時(shí)候發(fā)現(xiàn)還是不能請(qǐng)求到正確的方法,因?yàn)榈絪pring時(shí)已經(jīng)自動(dòng)解碼了。可以把%2F再編一次碼變成%252F。%編碼后是25
/** */abc/xiaoming/h5/user.json/get */ @ResponseBody @RequestMapping(method=RequestMethod.POST ,value="/abc/{username}/{resourceUrl}/{methodName}") public String dubboMock(HttpServletResponse response,@PathVariable String username,@PathVariable String resourceUrl,@PathVariable String methodName){ }
3、放棄使用PathVariable,手動(dòng)去處理
/** */abc/xiaoming/h5/user.json/get */ @ResponseBody @RequestMapping(method=RequestMethod.POST ,value="/abc/**") public String dubboMock(HttpServletResponse response,HttpServletResponse request){ String url = request.getRequestURI(); //處理url }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?Profile多環(huán)境配置方式
這篇文章主要介紹了SpringBoot?Profile多環(huán)境配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06關(guān)于Java鎖性能提高(鎖升級(jí))機(jī)制的總結(jié)
這篇文章主要介紹了關(guān)于Java鎖性能提高(鎖升級(jí))機(jī)制的總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05基于Spring中的事務(wù)@Transactional細(xì)節(jié)與易錯(cuò)點(diǎn)、幻讀
這篇文章主要介紹了基于Spring中的事務(wù)@Transactional細(xì)節(jié)與易錯(cuò)點(diǎn)、幻讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11AndroidHttpClient使用Cookie應(yīng)用分析
今天想把一個(gè)用使用了HttpClient的自動(dòng)簽到小程序移植到Android上,還好Android的SDK自帶了HttpClient的包.當(dāng)然也可以繼續(xù)使用DefaultHttpClient,但用為Android定制的AndroidHttpClient自然更好2012-11-11