2

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>?

du-it
  • 2,561
  • 8
  • 42
  • 80

1 Answers1

3

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

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • In my test I do mockMvc... .andExpect(status().isNoContent()); and it is successful in either way. So, when the return type is primitive void the HttpStatus seems to be send to the requestor as well. so why should I use ResponseEntity? what is the benefit? So, if I don't want to add any other header it doesn't matter which return type to use? The HttpStatus will be returned alway? – du-it Dec 21 '21 at 16:22
  • That's what I wrote in my answer: "You would need the ResponseEntity if you want to add some headers." – Simon Martinelli Dec 21 '21 at 16:28
  • @du-it in your case you can safely return void from controller's method – Nick Dec 21 '21 at 16:29