我想使用 Spring MVC 来构建一个 RESTful 应用程序。使用 POST 方法时出现 404 错误,但使用 GET 时没有。我相信在配置或控制器方法上一定有一些错误。
当我使用 GET 方法时,我将 JSON 数据返回给客户端,当我使用 POST 方法时,客户端将 JSON 数据发布到服务器,服务器应该将接收到的 JSON 数据发送回客户端。但是,GET 方法运行但 POST 方法得到 404 错误。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
version="3.0">
<display-name>JiecaoServer</display-name>
<servlet>
<servlet-name>jiecaoServer</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jiecaoServer</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
jiecaoServer-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- mvc:annotation-driven /-->
<context:component-scan base-package="jiecao.server.controller" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
</beans>
控制器类是 RestfulTest.java
package jiecao.server.controller;
import java.util.HashMap;
import jiecao.server.dao.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class RestfulTest {
@RequestMapping(method=RequestMethod.GET, value="/test/{id}")
@ResponseBody
public User getById(@PathVariable int id){
User u = new User();
u.setId(id);
u.setNickname("asdawdawd");
return u;
}
@RequestMapping(method=RequestMethod.POST, value="/test",
headers="Accept=application/json")
public @ResponseBody User addUser(@RequestBody HashMap msg){
System.out.println("Hererererererere");
//user.setName(user.getNickname());
User user = new User();
user.setId(Integer.parseInt((String)msg.get("id")));
user.setNickname((String)msg.get("nickname"));
return user;
}
}