ResponseEntity
We use ResponseEntity
class to predefine a response with correct header. So that we only need to provide the body.
For example:
@PostMapping(value = "/v{version}/meow", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Callable<ResponseEntity<MeowRequest>> greet(@PathVariable String version,
@RequestBody @Valid MeowRequest request) {
return () -> ResponseEntity.ok(request);
}
Or in the case of error:
@ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class)
@ResponseBody
public ResponseEntity<Object> handleValidationException(org.springframework.http.converter.HttpMessageNotReadableException ex) {
return ResponseEntity.status(400).body(new Object() {
public final String error = "Bad Request for greet";
public final String message = ex.getMessage();
});
}