Spring Cloud OpenFeign增加解码器,支持非标准MIME Content-Type:text/json

场景描述

在使用open feign调用某服务时,接口操作报出如下异常

2022-03-07 17:53:18.453 [http-nio-9999-exec-1] ERROR [org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]175] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.codec.DecodeException: Could not extract response: no suitable HttpMessageConverter found for response type [class cn.monyun.api.response.MonYunResponse] and content type [text/json;charset=utf-8]] with root cause [TID: N/A] 
org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class cn.monyun.api.response.MonYunResponse] and content type [text/json;charset=utf-8]

乍一看,有点懵,按照官方文档显示返回值为json,但是jackson转换失败,仔细查看接口返回content-type为text/json;charset=utf-8,想了半天没见过这种类型,经过一顿查找,最后发现这个类型竟然是山寨版。。官方版本应为 application/json,但是没办法,对方系统已经在线上运行了,只能添加解析器,支持一下这种类型转换

处理步骤

新增消息转换器,需要兼容支持text/json

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chunyang.leng
 * @date 2022-03-07 7:17 PM
 */
@Configuration
public class TextMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {

    private static final Logger logger =  LoggerFactory.getLogger(TextMappingJackson2HttpMessageConverter.class);

    public TextMappingJackson2HttpMessageConverter() {
        // 先将原先支持的MediaType列表拷出
        List<MediaType> mediaTypeList = new ArrayList<>(this.getSupportedMediaTypes());
        //加入对text/plain的支持
        mediaTypeList.add(new MediaType("text", "json", StandardCharsets.UTF_8));
        //将已经加入了text/json的MediaType支持列表设置为其支持的媒体类型列表
        this.setSupportedMediaTypes(mediaTypeList);

        logger.info("==================> 启用 text/json系列消息转换器");

    }
}

增加 open feign 解码器

import feign.codec.Decoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;

private HttpMessageConverters messageConverters = null;
/**
     * 补充 http 协议消息转换器
     * content-type:text/json
     *
     * @param converter 自定义的消息转换器
     * @return openFeign 解码器
     */
    @Bean
    @ConditionalOnBean(TextMappingJackson2HttpMessageConverter.class)
    public Decoder feignDecoder(TextMappingJackson2HttpMessageConverter converter){
     if (messageConverters == null){
            messageConverters = new HttpMessageConverters(converter);
        }
        ObjectFactory<HttpMessageConverters> objectFactory = () -> messageConverters;
        logger.info("==================> 初始化openFeign text/json支持解析器");
        return new SpringDecoder(objectFactory);
    }

经过测试,已经可以正常转换

附录

标准的MIME类型链接 :
《百度百科》https://baike.baidu.com/item/MIME/2900607?fr=aladdin
《HTML4.01 规范》 https://www.w3.org/TR/html4/types.html#h-6.7

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

北漂的菜小白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值