网址格式为路径时,无法使Spring MVC调度程序正常工作
问题内容:
我有一个Web应用程序,目前我们仅将Spring
MVC用于REST服务。我们希望其余服务显示在之下${contextPath}/rest/**
,但是当我设置它时,我们得到:
在名称为“ Spring MVC Dispatcher Servlet”的DispatcherServlet中未找到带有URI [/ myapp /
rest / testSvc / message]的HTTP请求的映射
我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
,这很好,可以在启动时注册服务。
<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";
}
如果我cahnge的url-pattern
在web.xml
为*
.rest和我的请求映射message
到message.rest
它的工作原理。
问题答案:
问题可能是您/rest
在web.xml
和中都重复了前缀@RequestMapping
。它应该在一个或两个中的一个中,但不能同时在两个中,例如
<url-pattern>/rest</url-pattern>
和
@RequestMapping(value = "/testService")
在其上进行@RequestMapping
操作的路径是servlet部分之后的路径的一部分,您web.xml
将servlet部分定义为/path
,因此@RequestMapping
与剩下的任何内容(即)匹配/testService
。
在当前形式下,您@RequestMapping
实际上与匹配{contextPath}/rest/rest/testService
。