Bre*_*yan 6 java spring spring-mvc spring-3
我有一个Web应用程序,我们目前正在为REST服务应用spring MVC.我们希望我们的休息服务出现在下面${contextPath}/rest/**,但是当我设置它时,我们得到:
在DispatcherServlet中找不到带有URI [/ myapp/rest/testSvc/message]的HTTP请求的映射,名称为"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>
Run Code Online (Sandbox Code Playgroud)
servlet-context.xml,这很好,并在注册启动时注册服务.
<context:component-scan base-package="com.mycompany.myapp.rest" />
<mvc:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
我的控制器如下所示:
@Controller
@RequestMapping(value = "/rest/testService")
public class TestREST {
@RequestMapping(value="message", method=RequestMethod.GET)
public @ResponseBody String getMessage() {
return "REST working";
}
Run Code Online (Sandbox Code Playgroud)
如果我cahnge的url-pattern在web.xml为*.rest和我的请求映射message到message.rest它的工作原理.
ska*_*man 25
这个问题可能是你已经重复了/rest在这两个前缀web.xml和@RequestMapping.它应该在一个或两个中,但不是两个,例如
<url-pattern>/rest</url-pattern>
Run Code Online (Sandbox Code Playgroud)
和
@RequestMapping(value = "/testService")
Run Code Online (Sandbox Code Playgroud)
赖以路径@RequestMapping操作是如下的servlet的部分路径的部分,你web.xml定义了一个Servlet一部分/path,所以@RequestMapping比赛对无论是左,即/testService.
在目前的形式中,您@RequestMapping实际上是匹配的{contextPath}/rest/rest/testService.
| 归档时间: |
|
| 查看次数: |
17360 次 |
| 最近记录: |