feign 默认只支持application/json 类型的响应报文,如果响应的并非这种类型,则无法解析,此时需要自定义实现
案例如下:
package com.eno.config.feign;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
/**
*
* @ClassName: FeignMessageConverter
* @Description: 新增feign 消息解码类型支持
*/
public class FeignMessageConverter extends MappingJackson2HttpMessageConverter {
public FeignMessageConverter() {
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.TEXT_PLAIN);
mediaTypes.add(MediaType.TEXT_HTML);
setSupportedMediaTypes(mediaTypes);
}
}
2.自定义实现decoder
@Bean
public Decoder feignDecoder() {
FeignMessageConverter converter = new FeignMessageConverter();
ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(converter);
return new SpringDecoder(objectFactory);
}