Spring ResponseEntity : use constructor or static builder?

Viewed 13525

In a Spring Boot RestController, I'm using the springframework class ResponseEntity to return the response for the endpoint call.

I recently found out there are two ways to instantiate this class.

Using the constructor :

response = new ResponseEntity<MyDto>(myDto, myHeaders, HttpStatus.OK);

Using the static builder :

response = ResponseEntity.ok().headers(myHeaders).body(myDto);

The resulting instance seams to be the same.

I wonder, what are the pros and cons of each ? In which situation should I use preferably one or the other ?

3 Answers

So the answer seems to be "you can do indifferently one or the other, according to your personal code preferences"

Related