org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported或其他Content type不支持处理

很久没从头到尾搭框架,今天搭的过程中,springmvc controller方法入参用@RequestBody自动绑定参数时一直提示各种 not supported

排查问题有两个解决路径:

1)使用post协议提交时,请检查Content type类型,如: 

$.ajax({
    type: "POST",
    contentType: "application/json;charset=UTF-8",
    url: "/reg",
    data: JSON.stringify(data.field),
    dataType: ‘json‘,
    success: function(result) {
        if(result.code == 0) {
            layer.msg(‘注册成功!‘);
        } else {
            layer.msg(result.msg);
        }
    }
});

  请检查上方contentType类型,如果想用springmvc @RequestBody注解做提交json字符串自动绑定到pojo入参时,类型需要是"application/json;charset=UTF-8",否则会抛"not supported"异常。

2)缺少jackson-databind jar包

  这个好办,把maven或gradle的坐标加上就好,如下:

  maven:

    

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.8.1</version>
</dependency>

  gradle:

    

compile group: ‘com.fasterxml.jackson.core‘, name: ‘jackson-databind‘, version: ‘2.8.8.1‘

然后controller直接这么用就好了:

  

@RequestMapping(value = "/reg", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVo reg(@RequestBody user u) throws Exception {
       //其他crud逻辑
    }

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported或其他Content type不支持处理

时间: 2024-12-24 10:17:20

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported或其他Content type不支持处理的相关文章

org.springframework.web.HttpMediaTypeNotSupportedException: Content type &#39;application/json;charset=UTF-8&#39; not supported

踩到了一个神坑,明明@RequestMapping注解并没有设置consumes,即没有限定请求参数的类型.却出现文章底部的错误,原因竟然是同一个model下注解了两个@JsonBackReference. @Entity // 用户 public class User extends IDomain { @Column(columnDefinition = "varchar(255) comment '[电话]'") private String phone; @Column(col

SpringMVC在使用JSON时报错信息为:Content type &#39;application/json;charset=UTF-8&#39; not supported

直接原因是:我的(maven)项目parent父工程pom.xml缺少必要的三个jar包依赖坐标. 解决方法是:在web子模块的pom.xml里面添加springMVC使用JSON实现AJAX请求. <!--spring mvc-json依赖--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifact

springboot框架,文件上传问题===org.springframework.web.HttpMediaTypeNotSupportedException: Content type &#39;multipart/form-data;

使用IDEA开发springboot项目,需求中要提交数据项和文件上传,同时接收实体bean对象和文件,后台Controller接收配置方式: Controller代码如下: 1 @RequestMapping(value="/comment",method = RequestMethod.POST) 2 public @ResponseBody RetResult saveIndustryComment(HttpServletRequest request,@RequestParam

Spring MVC 前后台传递json格式数据 Content type &#39;application/x-www-form-urlencoded;charset=UTF-8&#39; not supported

报错如下: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported 解决方案: 引入如下包: 问题既解决. Spring MVC 前后台传递json格式数据 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

Content type &#39;application/x-www-form-urlencoded;charset=UTF-8&#39; not supported for @RequestBody XXX

问题描述 前端使用ajax发送post请求发送Json数据,spring mvc报Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported for @RequestBody XXX错误 解决办法 1.重新组装数据 var params=JSON.stringify({param1:param1}); 2.ajax请求设置 dataType: "json", data: param, cont

解决“Resource interpreted as Document but transferred with MIME type application/json”问题

在上传图片时,使用ajax提交,返回的数据格式为json.在测试时发现IE浏览器中,上传图片后,没有显示图片,而是弹出一个提示:是否保存UploadImg.json文件:而在其他浏览器中正常. 在Chrome中调试后发现,图片上传成功后,浏览器给出了一个警告:Resource interpreted as Document but transferred with MIME type application/json. 原来后台代码在返回json数据时,响应数据的ContentType默认为"a

Resource interpreted as Document but transferred with MIME type application/json

转自:https://blog.csdn.net/just_lover/article/details/81207472 我在修改并保存后,界面返回提示“undifine”,实际我是看到有返回提示的.控制台输出的是“Resource interpreted as Document but transferred with MIME type application/json ” 检查发现我在修改的时候并没有上传文件, 而表单的表头是<form id="dialogform" me

http post Content-type: application/json; charset=utf-8

  The header just denotes what the content is encoded in. It is not necessarily possible to deduce the type of the content from the content itself, i.e. you can't necessarily just look at the content and know what to do with it. That's what HTTP head

Webservice报错客户端发现响应内容类型为“application/json;charset=UTF-8”,但应为“text/xml”。

控制台对接Webservice正常,同样的方法在Web项目上报错: 客户端发现响应内容类型为“application/json;charset=UTF-8”,但应为“text/xml”.请求失败,错误信息为:--{ "code" : "03010250002", "msg" : "Illegal json string", "data" : ""}--. 说明: 执行当前 Web 请求期