How do I get the response headers and status of a void method?
See original GitHub issueHi,
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
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (1 by maintainers)
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’s not obvious how to retrieve response headers in Feign. It is not even mentioned in the documentation.
The question was re-posted as https://github.com/spring-cloud/spring-cloud-netflix/issues/668#issuecomment-160180184