我有一个网络应用程序,目前我们正在将 Spring MVC 应用到 REST 服务。我们希望我们的休息服务出现在${contextPath}/rest/**
,但是当我设置这个时,我们得到:
在名称为“Spring MVC Dispatcher Servlet”的 DispatcherServlet 中未找到带有 URI [/myapp/rest/testSvc/message] 的 HTTP 请求的映射
My web.xml
has:
<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
,这很好,并且正在注册服务,因为它们在启动时注册。
<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
in web.xml
到 *.rest 和我的请求映射message
to message.rest
有用。
问题可能是您重复了/rest
两者中的前缀web.xml
and @RequestMapping
。它应该是一个或其中之一,但不能同时存在,例如
<url-pattern>/rest</url-pattern>
and
@RequestMapping(value = "/testService")
所经过的路径@RequestMapping
操作是 servlet 部分之后的路径部分,并且您的web.xml
将 servlet 部分定义为/path
, so @RequestMapping
与剩下的任何内容匹配,即/testService
.
以目前的形式,您的@RequestMapping
实际上是匹配的{contextPath}/rest/rest/testService
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)