Could not find acceptable representation 原因探究

Could not find acceptable representation 原因探究

日志信息

2022-06-15 19:48:23.399  WARN 2208 --- [p-nio-80-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

日志是一条warn信息,但是下面的报错就是重新定向到/error路径了,因此可以断定项目出错的地方在这个warn类型日志上

概况

主要的功能是Excel文件下载,文件下载时设置MediaType但是设置之后却会出现错误,项目内其他json类型MediaType却可以正常返回不受影响,受影响的仅仅为下载接口。

分析

对代码进行debug,数据正常组装到ResponseEntity但是在返回的时候Spring的返回类型MediaType仅仅只有json类型。那么可能就时SpringBoot结果转换器出现问题。 查看以前覆写的WebMvcConfigurationSupport 当初为了支持Iong类型精度不丢失重写了结果转换器,当时仅考虑到json类型,对于其他类型忘记写了,因此埋下了隐患。

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
                SerializerFeature.WriteEnumUsingToString,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.DisableCircularReferenceDetect);
        fastJsonConfig.setSerializeFilters((ValueFilter) (o, s, source) -> {
            if ((source instanceof Long || source instanceof BigInteger) && source.toString().length() > 15) {
                return source.toString();
            } else {
                return null == source ? Constants.EMPTY : source;
            }
        });

        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
    }

可以看出转换器中仅仅支持的MediaType为json-utf-8类型那么。

解决方案

在上面的代码后面加上一下代码就可以,也就是新增一些转换器

       converters.add(new ByteArrayHttpMessageConverter());
       converters.add(new ResourceHttpMessageConverter());
       converters.add(new SourceHttpMessageConverter());
       converters.add(new AllEncompassingFormHttpMessageConverter());

HttpMessageConverter作用

以下为请求流程图

请求报文
HttpInputMessage
HttpMessageConverter
java对象
spring MVC

以下为返回流程图

返回报文
HttpOutPutMessage
HttpMessageConverter
java对象
spring MVC

请求和返回都需要有合适的消息转换器将不同类型(MediaType)的的消息如JSON,XML等转化为对应的格式

如请求来的时候@RequestBody注解就会使用json媒体类型。

而我们的错误也很明确,就是返回的类型仅仅为配置了fastjson类型,因此加上其他的转换器就可以解决这个问题。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
根据引用\[1\]中的日志信息,报错信息是"Could not find acceptable representation"。根据引用\[3\]中的解释,这个错误可能有两个原因。第一个原因是返回的字符串不符合json格式。第二个原因是当用户请求/login.html时,Spring会查找/login对应的控制器,并得到其返回的文档类型为application/json,然后判断它与后缀名.html文档类型是否匹配,如果不匹配,就会报HttpMediaTypeNotAcceptableException错误。根据引用\[2\]中的描述,这个错误可能与自己重写的toString方法有关。所以,可能的解决方案是检查返回的字符串是否符合json格式,并确保请求的文档类型与后缀名匹配。 #### 引用[.reference_title] - *1* [Could not find acceptable representation 原因探究](https://blog.csdn.net/sinat_35045195/article/details/125303660)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Could not find acceptable representation(已解决)](https://blog.csdn.net/qq_45864365/article/details/122382016)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天心有情

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

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

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

打赏作者

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

抵扣说明:

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

余额充值