项目中想用@RequestBody直接接收json串转成对象
网上查了使用方法,看着非常简单,不过经过测试很快发现页面直接报415错误。
<body>
<h1>HTTP Status 415 - </h1>
<HR size="1" noshade="noshade">
<p>
<b>type</b> Status report
</p>
<p>
<b>message</b>
<u></u>
</p>
<p>
<b>description</b>
<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>
</p>
<HR size="1" noshade="noshade">
<h3>Apache Tomcat/6.0.41</h3>
</body>
但是问题依然没有解决。
最后在《Spring in Action》里找到一个信息
有两个前提条件:
The request’sContent-Type
header must be set toapplication/json.
The JacksonJSON
library must be available on the application’s classpath.
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.8</version>
<type>jar</type>
</dependency>所以如果大家遇到了同样的问题,可以先排除一下这两个因素。------------------------------
还有一种情况,在以上两个条件都满足的情况下,还是报同样的错误。
在springmvc的配置文件中必须有:
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
SpringMVC @RequestBody 自动转json Http415错误
原文:http://blog.csdn.net/tiantiandjava/article/details/46125141