简体   繁体   English

Spring MVC中的404错误

[英]404 error in Spring MVC

I wanted to use Spring MVC to build a RESTful application. 我想使用Spring MVC构建RESTful应用程序。 I get 404 error when I use the POST method, but not with GET. 使用POST方法时出现404错误,但不使用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. 当我使用GET方法时,我将JSON数据返回给客户端,而当我使用POST方法时,客户端将JSON数据发布到服务器,服务器应将接收到的JSON数据发送回客户端。 However, the GET method runs but POST method gets 404 error. 但是,GET方法可以运行,但是POST方法却出现404错误。

web.xml 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 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 控制器类是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. 遵循Thiago的建议。 You might possibly remove the headers="Accept=application/json" completely just to verify if that is not the reason you are getting a 404. 您可能会完全删除headers =“ Accept = application / json”只是为了验证这是否不是获取404的原因。

My only other suggestion would be to download the spring source code and start debugging. 我唯一的其他建议是下载spring源代码并开始调试。 Start from the org.springframework.web.servlet.DispatcherServlet class. 从org.springframework.web.servlet.DispatcherServlet类开始。

Your @ReqestMapping is looking for a post with this http header: 您的@ReqestMapping正在寻找带有以下http标头的帖子:

headers="Accept=application/json"

Are you setting this header on your request? 您是否在请求中设置此标头? If not, you will get a 404. 如果没有,您将得到404。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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