public Decoder feignDecoder() { return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter())); } public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() { final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new GateWayMappingJackson2HttpMessageConverter()); return new ObjectFactory<HttpMessageConverters>() { @Override public HttpMessageConverters getObject() throws BeansException { return httpMessageConverters; } }; } public class GateWayMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter { GateWayMappingJackson2HttpMessageConverter(){ List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")); setSupportedMediaTypes(mediaTypes); } }
时间: 2024-02-15 11:09:04 浏览: 108
这段代码定义了 Feign 的 Decoder 和一个自定义的 HttpMessageConverter。在 feignDecoder 方法中,使用了 ResponseEntityDecoder 和 SpringDecoder,其中 SpringDecoder 使用了 feignHttpMessageConverter 方法返回的 HttpMessageConverters 对象。而 feignHttpMessageConverter 方法中,又定义了一个 GateWayMappingJackson2HttpMessageConverter 类,继承了 MappingJackson2HttpMessageConverter,重写了构造方法中的 setSupportedMediaTypes 方法,支持解析 MediaType 为 TEXT_HTML_VALUE + ";charset=UTF-8" 的响应报文。这样的目的是为了解决一些非标准的响应报文格式在 Spring Cloud Gateway 中的问题。
相关问题
package com.github.wxiaoqi.security.gate.config; import feign.codec.Decoder; import org.springframework.beans.BeansException; import org.springframework.beans.factory.ObjectFactory; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.openfeign.support.ResponseEntityDecoder; import org.springframework.cloud.openfeign.support.SpringDecoder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.WebClient; import java.util.ArrayList; import java.util.List; /** * @author ace * @create 2019/1/27. */ @Configuration public class GatewayConfig { /** * 负载均衡注解 * @return */ @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } @Bean public Decoder feignDecoder() { return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter())); } public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() { final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new GateWayMappingJackson2HttpMessageConverter()); return new ObjectFactory<HttpMessageConverters>() { @Override public HttpMessageConverters getObject() throws BeansException { return httpMessageConverters; } }; } public class GateWayMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter { GateWayMappingJackson2HttpMessageConverter(){ List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")); setSupportedMediaTypes(mediaTypes); } } @Bean @LoadBalanced public WebClient.Builder loadBalancedWebClientBuilder() { return WebClient.builder(); } }
这是一个 Spring Cloud Gateway 的配置类,其中包含了一些 Bean 的定义,例如负载均衡的 RestTemplate 和 WebClient.Builder。此外,还定义了一个 GateWayMappingJackson2HttpMessageConverter 类,用于处理响应报文的转换。同时,该类还使用了 Feign 的 Decoder 进行响应解码。
PettyCashResultVO pettyCashResultVO = remoteCamundaService.ccCreate(new URI("http://localhost:9999"),pettyCashRequestVO,SecurityConstants.FROM_IN);这行代码报错feign.codec.DecodeException: Could not extract response: no suitable HttpMessageConverter found for response type [class eeb.afc.whkg.cc.vo.PettyCashResultVO] and content type
这个错误通常是由于远程服务返回的响应内容类型与客户端期望的响应内容类型不匹配造成的。你可以尝试指定适当的响应内容类型,或者在客户端配置中添加适当的消息转换器来解决此问题。
例如,如果你期望的响应内容类型是 JSON 格式,可以在客户端配置中添加一个适当的 JSON 消息转换器:
```java
@Configuration
public class MyClientConfiguration {
@Bean
public Decoder feignDecoder() {
HttpMessageConverter<Object> converter = new MappingJackson2HttpMessageConverter();
ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(converter);
return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
}
}
```
这里使用了 `MappingJackson2HttpMessageConverter` 消息转换器来处理 JSON 格式的响应内容。你可以根据实际情况选择适当的消息转换器。
阅读全文