Openfeign How to use @Headers annotation with different produces and consumes types?

4.2k views Asked by At

I want to use Feign client in my project but some endpoints produces text/plain while consuming application/json. Right now I have something like this. Method is producing plain/text and will consume application/json. Is there a way to fix it? Note that this is not a Spring boot application. It's just a Java/Maven project.

@Headers("Content-Type: text/plain")
@RequestLine(value = "POST /containers/{id}/services")
String startService(@Param("id") String id, String serviceType);

In Jaxrs we use below headers to specify the mime types that this method or class uses.

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.TEXT_PLAIN)

I want to be able to do same thing but there is no Produces or consumes headers in feign client. There is only @Headers there you can specify content type. I wonder if this annotation's Content-Type is used for both consuming and producing, or just for consuming, or if you can specify different types for both producing and consuming?

1

There are 1 answers

0
gozluklu_marti On

@Headers content should look like this:

@Headers({
  "Content-Type: application/json",
  "Accept: text/plain",
})

With Accept header in place, you tell feign client to produce JSON (in your case it is serviceType passed into the method) and consume text/plain.