• SpringMVC @RequestBody 自动转json Http415错误


    转自: http://blog.csdn.net/tiantiandjava/article/details/46125141

    项目中想用@RequestBody直接接收json串转成对象

    网上查了使用方法,看着非常简单,不过经过测试很快发现页面直接报415错误。

    1. <body>  
    2.         <h1>HTTP Status 415 - </h1>  
    3.         <HR size="1" noshade="noshade">  
    4.             <p>  
    5.                 <b>type</b> Status report  
    6.             </p>  
    7.             <p>  
    8.                 <b>message</b>  
    9.                 <u></u>  
    10.             </p>  
    11.             <p>  
    12.                 <b>description</b>  
    13.                 <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.</u>  
    14.             </p>  
    15.          <HR size="1" noshade="noshade">  
    16.             <h3>Apache Tomcat/6.0.41</h3>  
    17. </body>  


    经过一通查,多半的解决方法实说header里的 Content-Type 一定 application/json

    但是问题依然没有解决。

    最后在《Spring in Action》里找到一个信息

    有两个前提条件:

    The request’sContent-Typeheader must be set toapplication/json.
    The JacksonJSONlibrary must be available on the application’s classpath. 

    我满足了第一个,所以在classpath中添加了一个jar。问题解决了。

    1.         <dependency>  
    2.             <groupId>com.fasterxml.jackson.core</groupId>  
    3.             <artifactId>jackson-databind</artifactId>  
    4.             <version>2.5.3</version>  
    5.         </dependency>  

    所以如果大家遇到了同样的问题,可以先排除一下这两个因素。

    ------------------------------

    还有一种情况,在以上两个条件都满足的情况下,还是报同样的错误。

    在springmvc的配置文件中必须有:

    1. <!-- 默认的注解映射的支持 -->  
    2. <mvc:annotation-driven />  


    如果没有这个配置也是会报这个错的!

    为什么会引入jackson-databind包呢,因为默认的配置会用到:

    1. com.fasterxml.jackson.databind.ObjectMapper  
            <mvc:message-converters>  
                        <bean  
                            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
                            <property name="objectMapper">  
                                <bean class="com.fasterxml.jackson.databind.ObjectMapper">  
                                    <property name="dateFormat">  
                                        <bean class="java.text.SimpleDateFormat">  
                                            <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
                                        </bean>  
                                    </property>  
                                </bean>  
                            </property>  
                        </bean>  
                    </mvc:message-converters>
    

     然后改过后,错误嘛又变成了400 description The request sent by the client was syntactically incorrect.

    这是因为我传入的json内容和我的接收这个请求的函数的参数不一致,不传josn也会报这个错

  • 相关阅读:
    Access-自定义控件TabControl
    Excel公式-求最低价网站名字
    Excel图表-太极图
    Excel图表-"DNA"图
    VB中的GDI编程-2 画笔
    leetcode
    leetcode
    leetcode
    leetcode
    leetcode
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/5760215.html
Copyright © 2020-2023  润新知