@PathVariable 在@GetMapping、@DeleteMapping URL 中间带有斜线
我无法解决这个问题 (@PathVariable 在@GetMapping、@DeleteMapping URL 中间有斜杠)请帮助我!
URL ex : aaa/test/111/l2323:sdfsd:23423423/bbb
- test/111/l2323:sdfsd:23423423 : string variable with slashes
- bbb : another variable
@GetMapping("aaa/**")
public @ResponseBody List<Dto> getAAA(HttpServletRequest request) {
...
}
@DeleteMapping("aaa/**/{bbb}")
public void deleteTest(HttpServletRequest request,
@PathVariable("bbb") String bbb) {
...
}
@GetMapping("aaa/**/{bbb}")
public Dto getTest(HttpServletRequest request,
@PathVariable("bbb") String bbb) {
...
}
private String extractSlashVariable(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);
}..
我该如何处理这个问题? 上面的代码不起作用... 请帮助我谢谢!!
I cant solve this problem
(@PathVariable with slashes in middle of @GetMapping, @DeleteMapping URL) help me please!
URL ex : aaa/test/111/l2323:sdfsd:23423423/bbb
- test/111/l2323:sdfsd:23423423 : string variable with slashes
- bbb : another variable
@GetMapping("aaa/**")
public @ResponseBody List<Dto> getAAA(HttpServletRequest request) {
...
}
@DeleteMapping("aaa/**/{bbb}")
public void deleteTest(HttpServletRequest request,
@PathVariable("bbb") String bbb) {
...
}
@GetMapping("aaa/**/{bbb}")
public Dto getTest(HttpServletRequest request,
@PathVariable("bbb") String bbb) {
...
}
private String extractSlashVariable(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);
}..
how can I handle this problems?
The above codes are not working...
Help me please Thank you!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个原则性的答案是——你不能。 URL 存在时假定有一个结构 - 每个部分都用斜杠分隔。一般来说,在 url 中包含包含斜杠的“变量”是无效的,变量不能跨越 url 的多个级别。如果路径参数可能包含有问题的字符(例如斜杠),则需要对它们进行 url 编码(
%2F
我相信是/
的编码版本)。如果你真的想破解它来工作 - 如果你确实将 ant 匹配策略设置为
ant_path_matcher
(允许匹配像/**/test
这样的路径),就像错误一样消息表明它可能允许您解析它。您将在两个 GET 之间出现歧义错误,这是不可能的,但只有其中之一并且删除可能有效。为此,只需将spring.mvc.pathmatch.matching-strategy=ant_path_matcher
添加到您的 application.properties 中。A principled answer would be - you can't. URLs exist with the assumption of a structure - each part being separated by a slash. Having a "variable" containing slashes in your url is an invalid thing to take in as a rule, a variable can't span multiple levels of the url. If a path parameter can contain problematic characters like slashes they need to be url-encoded (
%2F
I believe is the encoded version of/
).If you really want to just hack it to work - if you do set the ant matching strategy to
ant_path_matcher
(that allows matching paths like/**/test
) like the error message suggests it might allow you to parse it. You will get ambiguity error between your two GETs, that will be impossible, but having just one of them and the delete miiiight work. To do that just addspring.mvc.pathmatch.matching-strategy=ant_path_matcher
to your application.properties.