springcloud feign增加响应报文解码类型

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 消息解码类型支持
 * @author eno
 * @date :2020年1月7日 下午1:33:21
 */
public class FeignMessageConverter extends MappingJackson2HttpMessageConverter {

	public FeignMessageConverter() {
		List 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 objectFactory = () -> new HttpMessageConverters(converter);
		return new SpringDecoder(objectFactory);
		
	}

 

你可能感兴趣的:(java)