spring-bootspring-restcontrollerhttp-status-code-204response-entity

What is the difference between returning void and ResponseEntity<Void> in Spring Boot RestController?


I wonder what the difference is between the return value of a Spring Boot RestController if void shall be returned?

I can see do difference in a test in either way. It seems that even if I return void the HttpStatus.NO_CONTENT (204) is returned to the requester as well as if I use ResponseEntity<Void>.

So, when I return void from an endpoint the HttpStatus is returned anyway and it doesn't matter if I choose void or ResponseEntity<Void>?


Solution

  • Exactly. You would need the ResponseEntity<Void> if you want to add some headers.