zoukankan      html  css  js  c++  java
  • 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逻辑
        }


  • 相关阅读:
    初级知识点四——设计模式六大原则
    初级知识点三——面向对象的三大特性
    初级知识点二——C#值传递
    初级知识点一——C#中的值类型与引用类型
    Unity资源引用问题
    Git在windows上的设置详解
    Unity中接收服务器消息并广播事件的实现
    Sphinx 自动化文档
    MAC Pro 同时安装 Python2 和 Python3
    数据中心网络架构演进 — 云网融合
  • 原文地址:https://www.cnblogs.com/dbaxyx/p/6924459.html
Copyright © 2011-2022 走看看