0

I have a post method in my controller class to create an offer and i want to show response status of 201 i.e. created when hitting the api via postman. This can be achieved by two methods which are @ResponseStatus(HttpStatus.CREATED) and ResponseEntity. I wanted to know what is the difference between the two. I have put them in comments below.

@PostMapping("/offers")
**//@ResponseStatus(HttpStatus.CREATED)**
public ResponseEntity<Object> createOffer(@Valid @RequestBody Offer offer) {
    Offer uoffer =  offerService.createOffer(offer);
    
    URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{jobTitle}").
            buildAndExpand(uoffer.getJobTitle()).toUri();

    **return ResponseEntity.created(location).build();**
    
}
Aditya
  • 950
  • 8
  • 37
  • Possible duplicate of [When use ResponseEntity and @RestController for Spring RESTful applications](https://stackoverflow.com/questions/26549379/when-use-responseentityt-and-restcontroller-for-spring-restful-applications) – mukesh210 Oct 28 '18 at 15:53
  • Thanks for your reply – Aditya Oct 28 '18 at 16:12

1 Answers1

1

Use @ResponseStatus(HttpStatus.CREATED)

You can only get status 201.

But use ResponseEntity you can return different statuses in different circumstances.

Oktfolio
  • 176
  • 3