Could not find acceptable representation

报这个错有很多种原因,我这只是其中的一种,可能您报的跟我的不一样,当然本篇文章如果能帮到您,那再好不过了。

问题描述

Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

在这里插入图片描述
控制台如下:

在这里插入图片描述

406:HTTP协议状态码的一种(4xx表示客户端的问题),表示客户端无法解析服务端返回的内容。说白了就是后台的返回结果前台无法解析就报406错误。

接口代码如下:

import com.gzl.cn.demo.entity.Params;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {

    @GetMapping("/requestParm1")
    public void requestParm1(@RequestParam Integer id) {
        System.out.println("get带@RequestParam:" + id);
    }

    @GetMapping("/requestParm7")
    public Params requestParm7(Params params) {
        System.out.println(params);
        return params;
    }
}

一旦代码添加上这个属性绑定之后就会报错: 以下是mvc当中接受参数的时候,会进行访问如下,属于自定义的参数解析器。

import cn.hutool.core.date.DateUtil;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.beans.PropertyEditorSupport;
import java.util.Date;

@Configuration
public class Config {

    @Bean
    public RequestMappingHandlerAdapter webBindingInitializer() {
        RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
        adapter.setWebBindingInitializer(new WebBindingInitializer() {
            @Override
            public void initBinder(WebDataBinder binder) {
                binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
                    @Override
                    public void setAsText(String text) {
                        // DateUtil.parse是hutool当中的方法
                        setValue(DateUtil.parse(text));
                    }
                });

                // 如果是字符串类型,就去除字符串的前后空格
                binder.registerCustomEditor(String.class,
                        new StringTrimmerEditor(true));
            }
        });
        return adapter;
    }
}

解决过程

首先去掉参数解析之后就正常了,那问题应该就是出现在解析绑定这块。然后我进行打断点调试,发现这块打断点,然后访问接口照样是会进来的。说明这块代码并没有失效,那为什么406呢?

在这里插入图片描述
调整为如下即可解决,原因是RequestMappingHandlerAdapter 本身就在容器当中,我们属于是给他添加属性,所以一定要从容器里面来取,然后再放回容器当中。

import cn.hutool.core.date.DateUtil;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.beans.PropertyEditorSupport;
import java.util.Date;

@Configuration
public class Config {

    @Bean
    public RequestMappingHandlerAdapter webBindingInitializer(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
        requestMappingHandlerAdapter.setWebBindingInitializer(new WebBindingInitializer() {
            @Override
            public void initBinder(WebDataBinder binder) {
                binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
                    @Override
                    public void setAsText(String text) {
                        // DateUtil.parse是hutool当中的方法
                        setValue(DateUtil.parse(text));
                    }
                });

                // 如果是字符串类型,就去除字符串的前后空格
                binder.registerCustomEditor(String.class,
                        new StringTrimmerEditor(true));
            }
        });
        return requestMappingHandlerAdapter;
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怪 咖@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值