Spring 多个路径变量
Spring multiple path variables
在我的 Spring 控制器中,我尝试获取 3 个路径变量:
@RequestMapping("{language}/{country}/{term}/catalogue") - @PathVariable String language, @PathVariable String country, @PathVariable String term
不幸的是,servlet 无法识别它。
有多种方法可以绑定 URI,例如
@RequestMapping("**/catalogue")
和 @RequestMapping("{language}/{country}/catalogue")
都可以,但是如果使用第三个路径变量,它就会停止工作。
控制器本身也映射到特定路径。
路径变量有限制吗?是否有可能其他通配符(例如 @RequestMapping("**"))
会得到更高的评估?例如 2 个通配符比 3 个定义的值更具体。但是通配符应该是实践中的最后一个匹配选项。
关于出现的错误:
首先,使用通配符映射,它们将被匹配。当我禁用通配符映射时,会抛出一个 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)
控制器方法:
@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) {
如题,这里是controller的全部RequestMappings(不好意思,这里不能postcontroller的完整代码):
@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")
}
感谢您的帮助。
试试这个。不要忘记方法参数中路径变量声明中的 ("lang")
@RequestMapping(value = "/{lang}/{count}/{term}", method=RequestMethod.GET)
public ResponseEntity<?> getSomething(@PathVariable("lang") String lang, @PathVariable("count") String count, @PathVariable("term") String term) {
首先,您在控制器的 @RequestMapping
中缺少 xyz
末尾的 /
(斜线)(或开头的 /
每个方法的 @RequestMapping
)。这是您的 3 路径变量方法 从未 被调用的主要原因。
@RequestMapping(value = "{language}/{country}/{term}/catalogue", method = RequestMethod.GET)
但是,一旦你解决了这个问题,我想你可能会有另一个问题。根据 Spring 文档 (link here):
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.
这意味着当您启用通配符匹配时,即使您在控制器的 @RequestMapping
末尾添加了 /
,您的 3 路径变量方法也不会被调用,因为第一个Spring.
认为使用通配符更具体
请检查所有 @RequestMapping
是否缺少斜杠。我建议您始终以斜杠开头,这样更容易区分代码中的 URL 映射。
这可能是一个错误(已在 Spring 4.1 中修复):查看 SPR-6741。
如问题中所述,您在同一个控制器中:
- 具有 3 个路径变量的映射
- 一个"fallback"映射
/**
试试这个:
@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.
}
只需将其放入您的代码中即可:
@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
}
你会传递:
http://localhost:8080/api/Français/France/fr/catalogue
在我的 Spring 控制器中,我尝试获取 3 个路径变量:
@RequestMapping("{language}/{country}/{term}/catalogue") - @PathVariable String language, @PathVariable String country, @PathVariable String term
不幸的是,servlet 无法识别它。
有多种方法可以绑定 URI,例如
@RequestMapping("**/catalogue")
和 @RequestMapping("{language}/{country}/catalogue")
都可以,但是如果使用第三个路径变量,它就会停止工作。
控制器本身也映射到特定路径。
路径变量有限制吗?是否有可能其他通配符(例如 @RequestMapping("**"))
会得到更高的评估?例如 2 个通配符比 3 个定义的值更具体。但是通配符应该是实践中的最后一个匹配选项。
关于出现的错误:
首先,使用通配符映射,它们将被匹配。当我禁用通配符映射时,会抛出一个 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)
控制器方法:
@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) {
如题,这里是controller的全部RequestMappings(不好意思,这里不能postcontroller的完整代码):
@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")
}
感谢您的帮助。
试试这个。不要忘记方法参数中路径变量声明中的 ("lang")
@RequestMapping(value = "/{lang}/{count}/{term}", method=RequestMethod.GET)
public ResponseEntity<?> getSomething(@PathVariable("lang") String lang, @PathVariable("count") String count, @PathVariable("term") String term) {
首先,您在控制器的 @RequestMapping
中缺少 xyz
末尾的 /
(斜线)(或开头的 /
每个方法的 @RequestMapping
)。这是您的 3 路径变量方法 从未 被调用的主要原因。
@RequestMapping(value = "{language}/{country}/{term}/catalogue", method = RequestMethod.GET)
但是,一旦你解决了这个问题,我想你可能会有另一个问题。根据 Spring 文档 (link here):
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.
这意味着当您启用通配符匹配时,即使您在控制器的 @RequestMapping
末尾添加了 /
,您的 3 路径变量方法也不会被调用,因为第一个Spring.
请检查所有 @RequestMapping
是否缺少斜杠。我建议您始终以斜杠开头,这样更容易区分代码中的 URL 映射。
这可能是一个错误(已在 Spring 4.1 中修复):查看 SPR-6741。
如问题中所述,您在同一个控制器中:
- 具有 3 个路径变量的映射
- 一个"fallback"映射
/**
试试这个:
@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.
}
只需将其放入您的代码中即可:
@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
}
你会传递: http://localhost:8080/api/Français/France/fr/catalogue