我想使用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;
}
}
答案 0 :(得分:1)
与Thiago所说的一致。你可能会删除headers =&#34; Accept = application / json&#34;完全只是为了验证这是不是你得到404的原因。
我唯一的另一个建议是下载spring源代码并开始调试。从org.springframework.web.servlet.DispatcherServlet类开始。
答案 1 :(得分:0)
您的@ReqestMapping正在寻找包含此http标头的帖子:
headers="Accept=application/json"
您是否在请求中设置此标头?如果没有,你将获得404。