我正在使用SpringCloud开样来调用另一个不属于我们团队的微服务。当我定义这个冒充客户的时候。
@FeignClient(name="test, url="/test")
public interface MyFeignClient {
@GetMapping("/hello)
MyCustomRespone getValuesFromOtherService(@RequestParam String name, @RequestParam int id);
}调用它时,会发生异常:Spring假装:无法提取响应:没有为响应类型找到合适的HttpMessageConverter
然后我尝试加入来自io.github.openfeign的假杰克逊。
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
</dependency>但它仍然显示出同样的例外。然后,我注意到我所调用的rest的上下文类型是“text/html”,我可以使用来分析它,但是它似乎不是一个很好的方法。
那么有什么方法可以解决这个问题呢,请注意,我不能修改不属于out团队的api。
发布于 2021-09-27 12:34:31
由于您为所做的api调用获得了html类型的响应,因此需要为此编写自定义httpMessageConverter。另外,请确保在:super.setSupportedMediaTypes(类型)中更新支持的媒体类型;请参考:https://www.javadevjournal.com/spring/spring-http-message-converter/,您也可以尝试将消息提取为字符串并使用Gson转换器。例: GsonHttpMessageConverter
public class CustomHttpMessageConverter extends GsonHttpMessageConverter {
public MyGsonHttpMessageConverter() {
List<MediaType> types = Arrays.asList(
new MediaType("text", "html", DEFAULT_CHARSET)
);
super.setSupportedMediaTypes(types);
}}
发布于 2022-02-21 16:07:34
我也遇到过同样的问题,我通过创建一个定制的解码器来解决这个问题。
@Bean
public Decoder feignDecoder() {
return (response, type) -> {
String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));
JavaType javaType = TypeFactory.defaultInstance().constructType(type);
return new ObjectMapper().readValue(bodyStr, javaType);
};
}在此之后,我将假象配置为使用此配置。
@FeignClient(url = "https://myurl.com", name = "client-name", configuration = FeignCustomConfiguration.class)https://stackoverflow.com/questions/69342789
复制相似问题