当url模式是路径时,无法让Spring MVC调度程序正常工作 [英] Can't get Spring MVC dispatcher to work properly when url pattern is a path
问题描述
我有一个网络应用程序,我们目前正在为REST服务应用spring MVC。我们希望我们的休息服务显示在 $ {contextPath} / rest / **
下,但是当我设置它时,我们得到:
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:
在DispatcherServlet中找不到带有URI [/ myapp / rest / testSvc / message]的HTTP请求的映射,名称为Spring MVC Dispatcher Servlet
No mapping found for HTTP request with URI [/myapp/rest/testSvc/message] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'
我的 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
,这是罚款,并在注册启动时注册服务。
servlet-context.xml
, which is fine and is registering services as they get registered at startup.
<context:component-scan base-package="com.mycompany.myapp.rest" />
<mvc:annotation-driven />
我的控制器如下所示:
@Controller
@RequestMapping(value = "/rest/testService")
public class TestREST {
@RequestMapping(value="message", method=RequestMethod.GET)
public @ResponseBody String getMessage() {
return "REST working";
}
如果我改变 url-pattern
在 web.xml
到* .rest和我的请求映射消息
到 message.rest
它有效。
If I cahnge the url-pattern
in web.xml
to *.rest and my request-mapping for message
to message.rest
it works.
推荐答案
问题可能是你重复了 / rest
前缀在 web.xml
和 @RequestMapping
。它应该在一个或两个中,但不是两个,例如
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.
<url-pattern>/rest</url-pattern>
和
@RequestMapping(value = "/testService")
<$ c的路径$ c> @RequestMapping operations是servlet部分后面的路径的一部分,而 web.xml
将servlet部分定义为 / path
,所以 @RequestMapping
匹配剩下的东西,即 / 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
实际上与 {匹配contextPath} / rest / rest / testService
。
In its current form, your @RequestMapping
is actually matching against {contextPath}/rest/rest/testService
.
这篇关于当url模式是路径时,无法让Spring MVC调度程序正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!