spring boot @ PathVariable processing of passing and with backslash parameter
- 2021-08-28 20:06:24
- OfStack
I won't talk too much, let's look at the complete code ~
@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;
}
Supplement: The problem of PathVariable receiving parameter value with dot number in springboot
Problem
@RequestMapping(value = "/{version}",method = RequestMethod.GET)
public String demo(@PathVariable String version){
return version;
}
If version is 1.0. 0, it returns 1.0, which is not what we expect.
Solve
@RequestMapping(value = "/{version:.+}",method = RequestMethod.GET)
public String demo(@PathVariable String version){
return version;
}