openfeign客户端报错总结

1.SpringCloud集成Feign报错:feign.FeignException$NotFound: status 404 reading

解决:看看每个接口是否没有加上下文

 

2.no suitable HttpMessageConverter found for request type异常

解决:同下
 

//@Headers({"Content-Type: application/json;charset=UTF-8","Accept: application/json;charset=UTF-8"})
    @RequestMapping(value = "/micro-mall-admin/interiorProxy/goods/admin/product/getProductList",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map proxy(@RequestBody QueryProductParam param);

 

3.no suitable HttpMessageConverter found for response type异常

解决:增加配置类:

@Configuration
@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class FooClientConfig {
    @Autowired
    ObjectMapper objectMapper;
    @Bean
    public Decoder feignDecoder() {
        HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(objectMapper());
        List<MediaType> mediaTypes = new ArrayList<>();
        //mediaTypes.add((MediaType) converter.getSupportedMediaTypes());
        mediaTypes.add(MediaType.TEXT_PLAIN);
        mediaTypes.add(new MediaType("text", "json", Charset.forName("UTF-8")));
        mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        mediaTypes.add(MediaType.TEXT_HTML);
        mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        mediaTypes.add(new MediaType("text", "plain", Charset.forName("UTF-8")));
        ((MappingJackson2HttpMessageConverter) jacksonConverter).setSupportedMediaTypes(mediaTypes);
        ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter);

        return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
    }
    @Bean
    public Encoder feignEncoder(){
        HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(objectMapper());
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.TEXT_PLAIN);
        mediaTypes.add(new MediaType("text", "json", Charset.forName("UTF-8")));
        mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        mediaTypes.add(MediaType.TEXT_HTML);
        mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        mediaTypes.add(new MediaType("text", "plain", Charset.forName("UTF-8")));
        ((MappingJackson2HttpMessageConverter) jacksonConverter).setSupportedMediaTypes(mediaTypes);
        ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter);
        return new SpringEncoder(objectFactory);
    }


    public ObjectMapper objectMapper() {
        return Jackson2ObjectMapperBuilder.json()
                .serializationInclusion(JsonInclude.Include.NON_NULL)
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .timeZone(TimeZone.getTimeZone("Asia/Shanghai"))
                .build()
                .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
    }

    public ObjectMapper customObjectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        return objectMapper;
    }

 

 

3.java.util.UnknownFormatConversionException: Conversion = '"' 或'\'

解决:@Test时不要用printf打印包含转义字符的json

//log.info(JSON.toJSONString(a));
System.out.printf(""+a.toString());

 

 

4.服务端返回{"resultCode":"500","resultMsg":"Content type 'text/plain;charset=UTF-8' not supported"} 

解决:同2,在接口加上:

consumes = MediaType.APPLICATION_JSON_UTF8_VALUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值