Monday, 12 August 2013

404 error in Spring MVC

404 error in Spring MVC

I am a beginner to SpringMVC and I wanted to using it to build a RESTful
application.I got 404 error when I using POST method. But everything was
fine when I used GET method. I believe there must be some mistakes in
configuration or in the controller method. The situation is when I use GET
method, I return the json data to client and when I use POST method, the
client post json data to server, and server should send the received json
data back to client. However, the GET method runs good but POST method
gets 404 error. 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>
And the controller class is 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;
}
}

No comments:

Post a Comment