Java Spring 多路径变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27985399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring multiple path variables
提问by Noctem
In my Spring controller, I try to get 3 path variables:
在我的 Spring 控制器中,我尝试获取 3 个路径变量:
@RequestMapping("{language}/{country}/{term}/catalogue") - @PathVariable String language, @PathVariable String country, @PathVariable String term
Unfortunately this will not be recognized by the servlet.
不幸的是,这不会被 servlet 识别。
There are ways to bind the URI, e.g.@RequestMapping("**/catalogue")
and also @RequestMapping("{language}/{country}/catalogue")
will work, but with a third path variable the it stops working.
有一些方法可以绑定 URI,例如@RequestMapping("**/catalogue")
,也@RequestMapping("{language}/{country}/catalogue")
可以使用,但是使用第三个路径变量它会停止工作。
The controller itself is also mapped to a specific path.
控制器本身也映射到特定路径。
Is there a limit for path variables? Is it possible that other wildcards (e.g. @RequestMapping("**"))
will be higher evaluated? e.g. 2 wildcards more specific than 3 defined values. But wildcards should be the last matching option in praxis.
路径变量有限制吗?其他通配符是否有可能(例如@RequestMapping("**"))
会得到更高的评估?例如 2 个通配符比 3 个定义的值更具体。但通配符应该是实践中的最后一个匹配选项。
Regarding the appearing error:
First, with the wildcard mappings, they will be matched. When I disable the wildcard mappings a org.springframework.web.HttpRequestMethodNotSupportedException
error is thrown.
关于出现的错误:
首先,使用通配符映射,它们将被匹配。当我禁用通配符映射时,org.springframework.web.HttpRequestMethodNotSupportedException
会引发错误。
15:42:53,881 DEBUG [http-bio-18091-exec-31] (org.springframework.web.servlet.DispatcherServlet) - Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'errors/exception'; model is null
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.resolveHandlerMethod(AnnotationMethodHandlerAdapter.java:665)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:431)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at [device detection filter]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
The Controller method:
控制器方法:
@RequestMapping(value = "{language}/{country}/{term}/catalogue", method = RequestMethod.GET)
public ModelAndView catalogue(HttpServletRequest request, HttpServletResponse response, @PathVariable("language") String language, @PathVariable("country") String country, @PathVariable("term") String term, @RequestParam(value = "d", defaultValue = "") String device, @RequestParam(value = "embedded", defaultValue = "false") String embedded, @RequestParam(value = "id", defaultValue = "") String idString, @RequestParam(value = "nr", defaultValue = "") String nr) {
As ask, here are all RequestMappingsfrom the controller (Sorry, I can't post the complete code from the controller here):
问一下,这里有来自控制器的所有RequestMappings(对不起,我不能在这里发布控制器的完整代码):
@Controller
@RequestMapping("xyz/")
public class Controller {
@RequestMapping(value = "{language}/{country}/{term}/catalogue", method = RequestMethod.GET)
@RequestMapping("**")
@RequestMapping("{language}/{country}/product")
@RequestMapping("{language}/{country}/product-detail")
@RequestMapping("{language}/{country}/product-search")
@RequestMapping("{language}/{country}/dealer-search")
@RequestMapping("{language}/{country}/product-finder")
@RequestMapping("{language}/{country}/table")
@RequestMapping("**/languages")
@RequestMapping("**/chooseLanguages")
}
Thanks for help.
感谢帮助。
采纳答案by Brian Clozel
回答by Susie
Try this. Don't forget the ("lang")
in the path variable declaration in your method parameter
尝试这个。不要忘记("lang")
方法参数中的路径变量声明
@RequestMapping(value = "/{lang}/{count}/{term}", method=RequestMethod.GET)
public ResponseEntity<?> getSomething(@PathVariable("lang") String lang, @PathVariable("count") String count, @PathVariable("term") String term) {
回答by Federico Peralta Schaffner
First of all, you're missing a /
(slash) at the end of xyz
in the controller's @RequestMapping
(or a /
at the beggining of every method's @RequestMapping
). This is the main reason for your 3 path variable method being neverinvoked.
首先,您在控制器的/
末尾(或在每个方法的开头都缺少一个(斜杠))。这是您的 3 路径变量方法从未被调用的主要原因。xyz
@RequestMapping
/
@RequestMapping
@RequestMapping(value = "{language}/{country}/{term}/catalogue", method = RequestMethod.GET)
However, once you solve that, I think you might have another problem. According to Spring docs (link here):
但是,一旦你解决了这个问题,我想你可能会遇到另一个问题。根据 Spring 文档(此处链接):
When a URL matches multiple patterns, a sort is used to find the most specific match.
A pattern with a lower count of URI variables and wild cards is considered more specific.
当 URL 匹配多个模式时,将使用排序来查找最具体的匹配项。
URI 变量和通配符数量较少的模式被认为更具体。
This means that when you enable wildcard matching, even if you added the /
at the end of your controller's @RequestMapping
, your 3 path variable method wouldn't be invoked, because the one with wildcards is considered more specific by Spring.
这意味着当您启用通配符匹配时,即使您/
在控制器的末尾添加了,@RequestMapping
也不会调用您的 3 路径变量方法,因为 Spring 认为带有通配符的方法更具体。
Please check all @RequestMapping
s for missing slashes. I advise you to always begin with a slash, this way it's easier to distinguish URL mappings in your code.
请检查所有@RequestMapping
s 是否缺少斜线。我建议您始终以斜杠开头,这样更容易区分代码中的 URL 映射。
回答by AJEET SINGH
Try this:
尝试这个:
@RequestMapping(value = "/{lang}/{count}/{term}", method=RequestMethod.GET)
public ResponseEntity<?> getSomething(@PathVariable("lang") String lang, @PathVariable("count") String count, @PathVariable("term") String term) {
// Your code goes here.
}
回答by Abdellah Abair
Just put this in your code it would be work nice :
把它放在你的代码中它会很好用:
@GetMapping(value = "{language}/{country}/{term}/catalogue",produces = {"application/json"})
public @ResponseBody String test(@PathVariable String language,@PathVariable String country,@PathVariable String term) {
return language+"\t"+country+"\t"+term
}
You'd pass in: http://localhost:8080/api/Fran?ais/France/fr/catalogue
你会传入: http://localhost:8080/api/Fran?ais/France/fr/catalogue