Reputation: 6612
I can define a GET method in two ways:
public ResponseEntity<Pet> getPetById(Long id);
and
public Pet getPetById(Long id);
They seem to be equivalent, except that the first one involves more boilerplate code. So, what is the reason to use ResponseEntity and what advantages it brings?
Upvotes: 4
Views: 6554
Reputation: 393
It allows you to customize the HTTP response sent back to the client.
Advantages:
Disadvantages:
Upvotes: 1
Reputation: 51
The difference is quite easy to explain. When you use ResponseEntity, you have full control about the contents of your response. You can change your headers, status code, ... When you don't use ResponseEntity as the return type of a controller method, spring will "automagically" create a default ResponseEntity.
So the biggest advantage in using ResponseEntity is that you have full control. The disadvantage is that it is more verbose than letting Spring work its magic.
Upvotes: 5