Return HTTP code 200 from Spring REST API
If you use @RestController
it should return 200 by default.
But anyway, you can set a particular response status by @ResponseStatus
annotation (even if the methods returns void
) or you can return a custom response by ResponseEntity
.
EDIT: added error handling
For error handling, you can return a particular response entity:
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body("some body ");
or you can use @ExceptionHandler
:
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public void handleError(Exception ex) {
// TODO: log exception
}
If you are using spring:
@PostMapping(value = "/v1/notification")
public ResponseEntity handleNotifications(@RequestParam("notification") String itemid) {
// parse here the values
return ResponseEntity.ok().build();
//OR ResponseEntity.ok("body goes here");
}