# 两种方案解决no suitable HttpMessageConverter found for response type

两种方案解决no suitable HttpMessageConverter found for response type [class com.zybang.dsp.admin.vo.CounterVO] and content type [text/html;charset=ISO-8859-1]


问题:json转对象时出现的问题,大部分传递json都是用的application/json,但是对方给我们传了text/html格式的json,导致无法成功转对象

方案1

接收对方返回的数据时用string接收,接收之后自己在专程json。这样就能成功转成我们需要的对象,这种方案最简单,有效。

方案2

因为我们用的是

  public void configureMessageConverters(List> converters) {

        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue,
                SerializerFeature.QuoteFieldNames, SerializerFeature.DisableCircularReferenceDetect);
        fastConverter.setFastJsonConfig(fastJsonConfig);

        List fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        converters.add(fastConverter);
    }

所以在上面添加一种新的形式即可:
添加代码为:

fastMediaTypes.add(MediaType.parseMediaType(MediaType.TEXT_HTML_VALUE + ";charset=ISO-8859-1"));

你可能感兴趣的:(# 两种方案解决no suitable HttpMessageConverter found for response type)