1

When I use @PostMapping, I will give a CREATED(201) response status together by ResponseStatus annotation. The same as @DeleteMapping, @PutMapping, ect.

So, is there any way to set the default response status at different requestMapping?

Rana_S
  • 1,520
  • 2
  • 23
  • 39
steven.tong
  • 345
  • 3
  • 16
  • Possible duplicate of [How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?](https://stackoverflow.com/questions/16232833/how-to-respond-with-http-400-error-in-a-spring-mvc-responsebody-method-returnin) – Tom Jul 19 '17 at 06:08
  • Sorry, maybe there is something wrong with the description. I know how to return the status code by return ResponseEntity or use ResponseStatus annotation. What I want is whether there is a way to config something, make PostMapping annotation always return 201 without ResponseEntity or ResponseStatus annotation. – steven.tong Jul 19 '17 at 08:28
  • You can try to use `@ControllerAdvice` but I'm not sure if it will solve your problem – Tom Jul 19 '17 at 09:23

3 Answers3

0

You can use ResponseEntity to set the http response on each methods Example:

    ResponseEntity.status(status);

you can give your status for each method with it

Adib Rajiwate
  • 405
  • 4
  • 19
0

You can return ResponseEntity from a method of controller as your mapping response

Example code as follows:

@GetMapping("/get")
public @ResponseBody ResponseEntity<String> get() {
    return new ResponseEntity<String>("GET Response", HttpStatus.OK);
}

You can use same mechanism in @DeleteMapping, @PutMapping and others.

Zico
  • 2,349
  • 2
  • 22
  • 25
  • 1
    Hi, @Zico. Maybe there is something wrong with the description. I know how to return the status code by return ResponseEntity or use ResponseStatus annotation. What I want is whether there is a way to config something, make PostMapping always return 201 without ResponseEntity or ResponseStatus annotation. Thanks. – steven.tong Jul 19 '17 at 08:27
0

Or just add annotation @ResponseStatus:

@ResponseStatus(HttpStatus.OK)