I created an application using JHipster as a start template.
JHipster provides ExceptionTranslatoran annotated class @ControllerAdvicethat handles exception handling for errors generated by client requests in controllers. This class maps exception types to methods, and each method constructs and returns an object of type ErrorDTOthat is then serialized in JSON and sent to the client. I added my own method for easy use:
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorDTO processIllegalArgumentException(IllegalArgumentException exception) {
return new ErrorDTO(ErrorConstants.ERR_VALIDATION, exception.getMessage());
}
It works very well, and if an error occurs (in this case it is too short a value), I get this JSON on the client:
{
"message":"error.validation",
"description":"The request payload that defines the search terms should be at least 3 characters long.",
"fieldErrors":null
}
, Spring MVC. (, , ), json, , , :
{
"timestamp":"2015-11-27T15:08:32.897+0000",
"status":400,
"error":"Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message":"Required request body is missing: public java.lang.String ch.unine.directory.web.rest.SearchResource.search(java.lang.String)",
"path":"/api/search"
}
( ), Spring Boot/Spring MVC ? , , ?