在我的 Spring 控制器中,我尝试获取 3 个路径变量:
@RequestMapping("{language}/{country}/{term}/catalogue") - @PathVariable String language, @PathVariable String country, @PathVariable String term
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不会被 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 …Run Code Online (Sandbox Code Playgroud)