2

I'm using SpringCloud openfeign to call another micro service that isn't owned by our team. When I define this feignclient.

@FeignClient(name="test, url="/test")
public interface MyFeignClient {
 
  @GetMapping("/hello)
  MyCustomRespone getValuesFromOtherService(@RequestParam String name, @RequestParam int id);
}

When calling this, an exception occurs: Spring Feign: Could not extract response: no suitable HttpMessageConverter found for response type

Then I tried to add feign-jackson from io.github.openfeign

<dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-jackson</artifactId>
</dependency>

But it still shows the same exception. Then I noticed that the rest api I'm calling return's context-type is "text/html". I can analyze it by using ObjectMapper, but it doesn't seems to be a good way to do it.

So is there a way to fix this, Note that I cannot modify the api that not belong to out team.

Liem
  • 446
  • 1
  • 6
  • 20

2 Answers2

1

I've encountered exactly same issue and I've fixed it by creating a custom decoder wich uses Jackson

@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);
    };
}

and after that I've configure feign to use this configuration.

@FeignClient(url = "https://myurl.com", name = "client-name", configuration = FeignCustomConfiguration.class)
ricristian
  • 466
  • 4
  • 17
0

Since you get a response of type html for the api call you make, you will need to write your custom httpMessageConverter for this. Also please make sure to update the supported media types in: super.setSupportedMediaTypes(types); Please refer: https://www.javadevjournal.com/spring/spring-http-message-converter/ You could also try to extract the message as a string and make use of Gson converters. Eg: GsonHttpMessageConverter

public class CustomHttpMessageConverter extends GsonHttpMessageConverter {
public MyGsonHttpMessageConverter() {
    List<MediaType> types = Arrays.asList(
            new MediaType("text", "html", DEFAULT_CHARSET)
    );
    super.setSupportedMediaTypes(types);
}

}

balias
  • 499
  • 1
  • 4
  • 17