第一次写博客,以前都是在csdn上面看各位大神的经验解决问题
针对我遇到的远程调用 feign的问题:feign.codec.DecodeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.xxx.xxxx.xxxx.xxx.xxx] and content type [text/html;charset=utf-8]
查了ai说要在FeignClient接口的方法加produces = ...
@GetMapping(value = "/api/order/getOrderInfoByOrderNo/{orderNo}",produces = "application/json")
要不就是在api类的GetMapping上加produces = "application/json" 均无果
又或者是加一个配置类:
@Configuration
public class MyFeignConfig {
private final ObjectFactory<HttpMessageConverters> messageConverters;
public MyFeignConfig(ObjectFactory<HttpMessageConverters> messageConverters) {
this.messageConverters = messageConverters;
}
@Bean
public Request.Options requestOptions() {
return new Request.Options(5000, 10000); // 连接超时和读取超时分别为5秒和10秒
}
@Bean
public Retryer feignRetryer() {
return new Retryer.Default(100, 1000, 3); // 初始间隔100ms,最大间隔1秒,重试次数3次
}
}
加配置类要在你的FeignClient上引一下,configuration = My...ion.class,还是不行
最后,好兄弟丢给我一个配置类,在你的FeignClient接口的父包或者爷爷包 可能是个client,和这个爷爷包加一个同级的包config,里面加上这个配置类:
@Slf4j
@Configuration
public class FeignConfig {
@Bean("requestInterceptor")
public RequestInterceptor requestInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
// 1.使用RequestContextHolder拿到刚进来的请求数据
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (requestAttributes != null) {
// 老请求
HttpServletRequest request = requestAttributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
// 2.同步请求头的数据
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
if("content-type".equals(name)) {
continue;
}
template.header(name, values);
}
}
log.warn("请求token:{}", request.getHeader("token"));
}
}
};
}
}
问题解决,好兄弟牛逼!