question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

26reactions
piefelcommented, Jul 26, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I send a header with void response? - Stack Overflow
Try this. It returns your preferred header and status, without body. @ResponseStatus(HttpStatus.
Read more >
c# - Determining response codes from void functions OOP
Say for example the client PUTs an update to a resource representing me that changes my status to 'dead'.
Read more >
How to Set a Header on a Response with Spring 5 - Baeldung
In this section, we'll learn how to set headers on single endpoint responses using ServerHttpResponse, ResponseEntity or ServerResponse (for ...
Read more >
IHttpResponse::ClearHeaders Method - Microsoft Learn
The ClearHeaders method removes the existing HTTP headers and then adds a default set of headers to the response.
Read more >
Servlets - Server HTTP Response - Tutorialspoint
Methods to Set HTTP Response Header ; 13. void sendError(int sc, String msg). Sends an error response to the client using the specified...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found