简体   繁体   中英

404 error in Spring MVC

I wanted to use Spring MVC to build a RESTful application. I get 404 error when I use the POST method, but not with GET. I believe there must be some mistakes in configuration or in the controller method.

When I use GET method, I return the JSON data to the client and when I use POST method, the client posts JSON data to the server, and the server should send the received JSON data back to client. However, the GET method runs 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;
    }
}

Along the lines of what Thiago suggested. You might possibly remove the headers="Accept=application/json" completely just to verify if that is not the reason you are getting a 404.

My only other suggestion would be to download the spring source code and start debugging. Start from the org.springframework.web.servlet.DispatcherServlet class.

Your @ReqestMapping is looking for a post with this http header:

headers="Accept=application/json"

Are you setting this header on your request? If not, you will get a 404.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM