Feign: How do I get the response headers and status of a void method?

Created on 26 Nov 2015  路  6Comments  路  Source: OpenFeign/feign

Hi,

I'm trying to use a Feign-client to attach to a Spring Data-REST RestRepository.

@FeignClient("entity-das")
public interface EntityClient {

    @RequestMapping(value = "/entities", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    PagedResources<Resource<Entity>> listEntities(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestParam("sort") String sort);

    @RequestMapping(value = "/entities/{uuid}", params = {"uuid"}, method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    Resource<Entity> getEntity(@PathVariable("uuid") String uuid);

    @RequestMapping(value = "/entities", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    void createEntity(Entity entity);

    @RequestMapping(value = "/entities/{uuid}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    void updateEntity(@PathVariable("uuid") String uuid, Entity entity);

    @RequestMapping(value = "/entities/{uuid}", method = RequestMethod.DELETE, consumes = MediaType.APPLICATION_JSON_VALUE)
    void deleteEntity(@PathVariable("uuid") String uuid);
}

I would like to retrieve the status code and location header from the response for the void methods, but with a void method there is no way to get to the headers/status.

Changing the method signature form void to ResponseEntity<Entity> works, as log as the method returns a body.

Default, the POST, PUT and DELETE methods on the RestRepository do not return a body, but only a status and a location-header.
For the POST and PUT methods this can be changed by configuring the RestRepository with:

restConfiguration.setReturnBodyOnCreate(true);
restConfiguration.setReturnBodyOnUpdate(true);

Sadly this does not apply to the DELETE method. This one returns only a status code, but I do not know how to reach that. Changing the method signature from void to any of the following forms does not work:

  • ResponseEntity<Entity>
  • ResponseEntity<Void>
  • ResponseEntity<?>
  • ResponseEntity<Object>
  • ResponseEntity<String>

They all fail with a nullpointer exception because of the lack of response body.

Is there a way to retrieve the HTTP status code from a Feign method for a resource that returns no body?

Kind regards,
Bas

Most helpful comment

This looks like a very valid Feign question to me. The fact that the OP wanted to access a Spring Data REST endpoint is quite immaterial. It鈥檚 not obvious how to retrieve response headers in Feign. It is not even mentioned in the documentation.

All 6 comments

Before you ask: I'm using The Angel-SR3 Spring-Cloud release

Then you should be asking in spring-cloud-netflix

Hi Spencer,
Thinking this was about Feign, I posted it here.
I'll ask the question over there.
Thanks,
Bas

@bas-velthuizen can you post a link to your question in spring-cloud-netflix? I'm not using spring cloud and have the same question, and think this is in fact relevant to Feign.

This looks like a very valid Feign question to me. The fact that the OP wanted to access a Spring Data REST endpoint is quite immaterial. It鈥檚 not obvious how to retrieve response headers in Feign. It is not even mentioned in the documentation.

Was this page helpful?
0 / 5 - 0 ratings