How to get response in JSON format using @ExceptionHandler in Spring MVC
You can annotate the handler method with @ResponseBody
and return any object you want and it should be serialized to JSON (depending on your configuration of course). For instance:
public class Error { private String message; // Constructors, getters, setters, other properties ...}@ResponseBody@ResponseStatus(HttpStatus.BAD_REQUEST)@ExceptionHandler(MethodArgumentNotValidException.class)public Error handleValidationException(MethodArgumentNotValidException e) { // Optionally do additional things with the exception, for example map // individual field errors (from e.getBindingResult()) to the Error object return new Error("Invalid data");}
which should produce response with HTTP 400 code and following body:
{ "message": "Invalid data"}
Also see Spring JavaDoc for @ExceptionHandler
which lists possible return types, one of which is:
@ResponseBody
annotated methods (Servlet-only) to set the response content. The return value will be converted to the response stream using message converters.
Replace
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Error in the process")
by
@ResponseStatus(value = HttpStatus.NOT_FOUND)
the 'reason' attribute force html render!I've waste 1 day on that.....
ResponseEntity class will serialize you JSON response by default here andthrow RuntimeException wherever you want to in application
throw RuntimeException("Bad Request")
Write GlobalExceptionHandler class
@ControllerAdvicepublic class GlobalExceptionHandler extends ResponseEntityExceptionHandler { private class JsonResponse { String message; int httpStatus ; public JsonResponse() { } public JsonResponse(String message, int httpStatus ) { super(); this.message = message; this.httpStatus = httpStatus; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public int getHttpStatus() { return httpStatus; } public void setHttpStatus(int httpStatus) { this.httpStatus = httpStatus; } } @ExceptionHandler({ RuntimeException.class }) public ResponseEntity<JsonResponse> handleRuntimeException( Exception ex, WebRequest request) { return new ResponseEntity<JsonResponse>( new JsonResponse(ex.getMessage(), 400 ), new HttpHeaders(), HttpStatus.BAD_REQUEST); }}
Output:
{ "message": "Bad Request", "httpStatus": 400}