转自: 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

SpringMVC @RequestBody 自动转json Http415错误的更多相关文章

  1. Spring MVC @RequestBody自动转JSON HTTP415错误解决方法

    转自:http://blog.csdn.net/tiantiandjava/article/details/46125141 项目中想用@RequestBody直接接收json串转成对象 网上查了使用 ...

  2. springmvc @responseBody自动打包json出现错误(外键查询死循环)问题

    在外键字段的get方法上加入@JsonIgnore

  3. SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法

    最近angularjs post到后台 400一头雾水 没有任何错误. 最后发现好文,感谢作者 SpringMVC中出现" 400 Bad Request "错误(用@Respon ...

  4. 解决ajax请求(SpringMVC后台)响应415/400/405错误

    解决ajax请求(SpringMVC后台)响应415/400/405错误 后端代码 bean public class user { private String username; private ...

  5. SpringMVC 中整合之JSON、XML

    每次看到好的博客我就想好好的整理起来,便于后面自己复习,同时也共享给网络上的伙伴们! 博客地址: springMVC整合Jaxb2.xStream:  http://www.cnblogs.com/h ...

  6. 扩展SpringMVC以支持绑定JSON格式的请求参数

    此方案是把请求参数(JSON字符串)绑定到java对象,,@RequestBody是绑定内容体到java对象的. 问题描述: <span style="font-size: x-sma ...

  7. java框架之SpringBoot(5)-SpringMVC的自动配置

    本篇文章内容详细可参考官方文档第 29 节. SpringMVC介绍 SpringBoot 非常适合 Web 应用程序开发.可以使用嵌入式 Tomcat,Jetty,Undertow 或 Netty ...

  8. SpringMVC札集(07)——JSON数据

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  9. 7、springmvc的自动配置

    1.springmvc的自动配置 文档:https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot ...

随机推荐

  1. session 丢失问题

    1. 存到memcached中, 十分简单, 在使用session之前, 加入下面两行代码 int_set('session.save_handler', 'memcache'); int_set(' ...

  2. uoj#87. mx的仙人掌

    //Achen #include<bits/stdc++.h> #define For(i,a,b) for(int i=(a);i<=(b);i++) #define Rep(i, ...

  3. 关于本地模块安装入maven仓库出现的异常

    Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.9.RELEASE:repackage (de ...

  4. 使用case语句给字体改变颜色

    使用case语句给字体改变颜色 #!/bin/bash color(){ RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1; ...

  5. Linux应用函数 -- 字符串

    1.strchr 原型 char *strchr(const char* _Str,char _Val) 头文件 string.h 功能 查找字符串_Str中首次出现字符_Val的位置 返回值  成功 ...

  6. Day3-Python基础3---函数介绍

    一.函数基本语法及特性 函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做sub ...

  7. sql语句优化方案

    1. 为查询缓存优化你的查询 NOW() 和 RAND() 或是其它的诸如此类的SQL函数都不会开启查询缓存,因为这些函数的返回是会不定的易变的. 所以,你所需要的就是用一个变量来代替MySQL的函数 ...

  8. apache将不带www域名301重定向到带www的域名的配置方法

    #强制重定向到wwwRewriteEngine OnRewriteCond %{HTTP_HOST} ^jb51.net/ [NC]RewriteRule ^(.*)$ http://www.jb51 ...

  9. Compare and Swap(CAS)

    CAS(Compare and Swap)是个原子操作.拿到一个新值后,CAS将其与内存中的值进行比较,若内存中的值和这个值不一样,则将这个值写入内存,否则,不做操作.在Java的 java.util ...

  10. asp.net mysql 链接类

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;u ...