异常org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

这是后端代码

    @PostMapping("/apk/selectAllApk")
    @ResponseBody
    public R selectAllApk(@RequestBody(required = false) Apk apk) {
        R r = new R();
        List<Apk> apks = apkFeign.selectAllList(apk);
        return r.ok(apks);
    }

前端代码

    <script th:inline="none">
        layui.use('table', function () {
            var table = layui.table;
            table.render({
                elem: '#table'
                , height: 312
                , contentType: 'application/json'//添加这句话
                , method: 'POST'
                , url: '/apk/selectAllApk' //数据接口
                , cols: [
                    [
                        {field: 'id', width: 80, title: 'ID', sort: true}
                        , {field: 'name', width: 80, title: '用户名'}
                        , {field: 'size', width: 80, title: '性别', sort: true}
                        , {field: 'apkUrl', width: 80, title: '城市'}
                        , {field: 'packageName', title: '签名', width: '30%', minWidth: 100}
                        , {field: 'apkMd5', title: '积分', sort: true}
                        , {field: 'apkVersion', title: '评分', sort: true}
                        , {field: 'createdTime', title: '职业'}
                        , {field: 'updatedTime', width: 137, title: '财富', sort: true}
                    ]
                ]
            });

        });

报错信息

服务端发生异常
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

你F12看看你的溜浏览器,你的请求体是不是json,ContentType头是不是application/json

请求体是json

你那个是响应头,是json。你的请求头不是,你看下面Request Headers

呃呃呃 请求头是这个
Content-Type:application/x-www-form-urlencoded; charset=UTF-8

不过我不是前端加上了contentType: 'application/json’吗

你加了,没生效。那是你的问题,要么是添加方式不对,要么是前端框架的BUG。反正浏览器和后盾程序不会骗你,你的请求头不对。

感谢感谢 :handshake: