我有一个 Web 应用程序,目前我们正在为 REST 服务应用 Spring MVC。我们希望我们的休息服务出现在 ${contextPath}/rest/**
下,但是当我设置它时,我们得到:
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
,很好,并且在启动时注册服务。
<context:component-scan base-package="com.mycompany.myapp.rest" />
<mvc:annotation-driven />
我的 Controller 如下所示:
@Controller
@RequestMapping(value = "/rest/testService")
public class TestREST {
@RequestMapping(value="message", method=RequestMethod.GET)
public @ResponseBody String getMessage() {
return "REST working";
}
如果我将 web.xml
中的 url-pattern
更改为 *.rest,并将 message
的请求映射更改为 message.rest
它有效。
最佳答案
问题可能是您在 web.xml
和 @RequestMapping
中重复了 /rest
前缀。它应该在其中一个中,但不能同时在两个中,例如
<url-pattern>/rest</url-pattern>
和
@RequestMapping(value = "/testService")
@RequestMapping
操作的路径是servlet部分之后的路径部分,你的web.xml
将servlet部分定义为/path
,所以 @RequestMapping
匹配剩下的任何内容,即 /testService
。
在当前形式中,您的 @RequestMapping
实际上与 {contextPath}/rest/rest/testService
匹配。
关于java - 当 url 模式是路径时,无法让 Spring MVC 调度程序正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5367309/