java 当 url 模式是路径时,无法让 Spring MVC 调度程序正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5367309/
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
Can't get Spring MVC dispatcher to work properly when url pattern is a path
提问by Brett Ryan
I have a web app that we're applying spring MVC just for REST services at the moment. We want our rest services to appear under ${contextPath}/rest/**
, however when I set this we get:
我有一个 Web 应用程序,我们目前正在将 Spring MVC 应用于 REST 服务。我们希望我们的休息服务出现在 下${contextPath}/rest/**
,但是当我设置它时,我们得到:
No mapping found for HTTP request with URI [/myapp/rest/testSvc/message] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'
在名为“Spring MVC Dispatcher Servlet”的 DispatcherServlet 中找不到带有 URI [/myapp/rest/testSvc/message] 的 HTTP 请求的映射
My web.xml
has:
我的web.xml
有:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/rest</url-pattern>
</servlet-mapping>
servlet-context.xml
, which is fine and is registering services as they get registered at startup.
servlet-context.xml
,这很好,并且正在注册服务,因为它们在启动时注册。
<context:component-scan base-package="com.mycompany.myapp.rest" />
<mvc:annotation-driven />
My controller looks as follows:
我的控制器如下所示:
@Controller
@RequestMapping(value = "/rest/testService")
public class TestREST {
@RequestMapping(value="message", method=RequestMethod.GET)
public @ResponseBody String getMessage() {
return "REST working";
}
If I cahnge the url-pattern
in web.xml
to *.rest and my request-mapping for message
to message.rest
it works.
如果我cahnge的url-pattern
在web.xml
为* .rest和我的请求映射message
到message.rest
它的工作原理。
回答by skaffman
The problem is likely that you have repeated the /rest
prefix in both web.xml
and @RequestMapping
. It should be in one or the either, but not both, e.g.
问题很可能是您/rest
在web.xml
和 中都重复了前缀@RequestMapping
。它应该在一个或两个中,但不能同时存在,例如
<url-pattern>/rest</url-pattern>
and
和
@RequestMapping(value = "/testService")
The paths upon which @RequestMapping
operates are the parts of the path that follows the servlet part, and your web.xml
defines the servlet part as /path
, so @RequestMapping
matches against whatever is left, i.e. /testService
.
赖以路径@RequestMapping
操作是如下的servlet的部分路径的部分,你web.xml
定义了一个Servlet一部分/path
,因此@RequestMapping
对无论是左,即相匹配/testService
。
In its current form, your @RequestMapping
is actually matching against {contextPath}/rest/rest/testService
.
在目前的形式中,您@RequestMapping
实际上是与{contextPath}/rest/rest/testService
.
回答by Raghuram
Perhaps you could try changing to <url-pattern>/rest/*</url-pattern
> or <url-pattern>/rest*</url-pattern
> and see if that helps.
也许您可以尝试更改为<url-pattern>/rest/*</url-pattern
> 或<url-pattern>/rest*</url-pattern
> 看看是否有帮助。