这是我的控制器
@Controller
public class UserController {
@RequestMapping(value = "/user", method = RequestMethod.GET,headers="Accept=application/json")
public @ResponseBody RegisterUser getUser() {
RegisterUser registerUser = new RegisterUser();
registerUser.setId(1);
registerUser.setName("John");
registerUser.setDob("15-04-1993");
registerUser.setAddress("US");
registerUser.setDepartment("IT");
return registerUser;
}
}
这是我的spring-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.user.test" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
这是我的web.xml文件
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
当我收到用户详细信息时,我的提前休息客户端出现了404错误。 我在控制台窗口中收到的错误是:
2017年1月8日下午3:02:18 org.springframework.web.servlet.PageNotFound handleNoSuchRequestHandlingMethod警告:没有匹配的处理程序方法 找到servlet请求:路径&#39; / user&#39;,方法&#39; GET&#39;,参数 地图[[空]]
答案 0 :(得分:0)
Your web.xml is not complete
**Add the following into your web.xml which creates Spring Container shared by all Servlets and Filters**
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>