spring 使用哪种模型 class 用于 Request Mapping 方法的错误响应?
Which model class does spring use for error responses of Request Mapping methods?
我正在为我的 @RequestMapping
方法的自定义错误场景设计一个 POJO。
Spring 默认情况下抛出 HttpMessageNotReadableException
如果 POST
没有任何 body 和 returns 错误响应非常好,就像这样:
{
"timestamp": "2017-07-28T18:54:11.867+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Required request body is missing: public org.springframework.http.ResponseEntity<some.package.name.SomeResponseClass> some.package.name.SomeControllerClass.someRequestMappingMethod(java.util.Map<java.lang.String, java.lang.String>, some.package.name.SomeRequestClass)",
"path": "/somepath/"
}
同样,如果对任何用 @Valid
注释的字段的验证失败,它会抛出 MethodArgumentNotValidException
和 returns 错误响应:
{
"timestamp": "2017-07-28T17:23:53.102+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"errors": [
{
"codes": [
"NotNull.incident.someField",
"NotNull.someField",
"NotNull.java.lang.String",
"NotNull"
],
"arguments": [
{
"codes": [
"someObject.someField",
"someField"
],
"arguments": null,
"defaultMessage": "someField",
"code": "someField"
}
],
"defaultMessage": "someField is null",
"objectName": "someObject",
"field": "someField",
"rejectedValue": null,
"bindingFailure": false,
"code": "NotNull"
}
],
"message": "Validation failed for object='someObject'. Error count: 2",
"path": "/somepath/"
}
我了解到,在上述任一情况下,Spring 必须返回 ResponseEntity
object,其中包含 headers 和 body它。
并且必须有一个模型 class 表示 body 具有如上所示的字段,即 timestamp
、status
、error
等。我只是想访问那个 class 以了解所有字段 Spring 已包含在其错误响应模型中的内容。或者可能是我可能想要扩展它。到目前为止,我已经花费了大量时间来搜索 class,但找不到。
我能找到的最好的是 ResponseEntityExceptionHandler#handleException()
,但我的调试器甚至不会到达那里设置的 break-point!
任何人都可以指出它所在的包吗?
对于Spring Boot 1.5.2.RELEASE / Spring 4.3.7
这是一个LinkedHashMap<String, Object>
。
通过搜索 ErrorController 找到它 - 有一个 class org.springframework.boot.autoconfigure.web.BasicErrorController
它的方法 error
在发生错误时被调用,它看起来像这样:
@RequestMapping
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
您可以深入研究方法 getErrorAttributes
以了解地图的构建方式。
下面的 class 会让您了解在发生错误时如何生成响应
org.springframework.boot.autoconfigure.web.DefaultErrorAttributes
参考:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling
此外,如果您想在出现错误时自定义响应,您也可以使用以下方法
@ControllerAdvice 和@ExceptionHandler
另一个 class 参考错误处理
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
我正在为我的 @RequestMapping
方法的自定义错误场景设计一个 POJO。
Spring 默认情况下抛出 HttpMessageNotReadableException
如果 POST
没有任何 body 和 returns 错误响应非常好,就像这样:
{
"timestamp": "2017-07-28T18:54:11.867+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Required request body is missing: public org.springframework.http.ResponseEntity<some.package.name.SomeResponseClass> some.package.name.SomeControllerClass.someRequestMappingMethod(java.util.Map<java.lang.String, java.lang.String>, some.package.name.SomeRequestClass)",
"path": "/somepath/"
}
同样,如果对任何用 @Valid
注释的字段的验证失败,它会抛出 MethodArgumentNotValidException
和 returns 错误响应:
{
"timestamp": "2017-07-28T17:23:53.102+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"errors": [
{
"codes": [
"NotNull.incident.someField",
"NotNull.someField",
"NotNull.java.lang.String",
"NotNull"
],
"arguments": [
{
"codes": [
"someObject.someField",
"someField"
],
"arguments": null,
"defaultMessage": "someField",
"code": "someField"
}
],
"defaultMessage": "someField is null",
"objectName": "someObject",
"field": "someField",
"rejectedValue": null,
"bindingFailure": false,
"code": "NotNull"
}
],
"message": "Validation failed for object='someObject'. Error count: 2",
"path": "/somepath/"
}
我了解到,在上述任一情况下,Spring 必须返回 ResponseEntity
object,其中包含 headers 和 body它。
并且必须有一个模型 class 表示 body 具有如上所示的字段,即 timestamp
、status
、error
等。我只是想访问那个 class 以了解所有字段 Spring 已包含在其错误响应模型中的内容。或者可能是我可能想要扩展它。到目前为止,我已经花费了大量时间来搜索 class,但找不到。
我能找到的最好的是 ResponseEntityExceptionHandler#handleException()
,但我的调试器甚至不会到达那里设置的 break-point!
任何人都可以指出它所在的包吗?
对于Spring Boot 1.5.2.RELEASE / Spring 4.3.7
这是一个LinkedHashMap<String, Object>
。
通过搜索 ErrorController 找到它 - 有一个 class org.springframework.boot.autoconfigure.web.BasicErrorController
它的方法 error
在发生错误时被调用,它看起来像这样:
@RequestMapping
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
您可以深入研究方法 getErrorAttributes
以了解地图的构建方式。
下面的 class 会让您了解在发生错误时如何生成响应 org.springframework.boot.autoconfigure.web.DefaultErrorAttributes
参考:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling
此外,如果您想在出现错误时自定义响应,您也可以使用以下方法 @ControllerAdvice 和@ExceptionHandler
另一个 class 参考错误处理 org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler