Java spring使用哪个模型类来响应请求映射方法的错误?

Java spring使用哪个模型类来响应请求映射方法的错误?,java,spring,spring-mvc,exception-handling,response,Java,Spring,Spring Mvc,Exception Handling,Response,我正在为我的@RequestMapping方法的自定义错误场景设计一个POJO 如果在没有任何主体的情况下生成POST,Spring默认会抛出httpMessageNodeEnableException,并很好地返回错误响应,如下所示: { "timestamp": "2017-07-28T18:54:11.867+0000", "status": 400, "error": "Bad Request", "exception": "org.springframe

我正在为我的
@RequestMapping
方法的自定义错误场景设计一个POJO

如果在没有任何主体的情况下生成
POST
,Spring默认会抛出
httpMessageNodeEnableException
,并很好地返回错误响应,如下所示:

{
    "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/"
}
@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);
}
我了解到,在上述任何一种情况下,Spring都必须返回一个
ResponseEntity
对象,其中包含头和主体。 必须有一个模型类来表示主体,该类具有类似于上面的字段,即
时间戳
状态
错误
等等。我只想访问该类,了解Spring在其错误响应模型中包含的所有字段。或者我可能想扩展它。到目前为止,我已经花了相当长的时间搜索该类,但找不到

我能找到的最好的方法是
ResponseEntityExceptionHandler#handleException()
,但我的调试器甚至不会在那里设置断点


有人能告诉我它所在的软件包吗?

用于Spring Boot 1.5.2.RELEASE/Spring 4.3.7

这是一个
LinkedHashMap

通过搜索ErrorController找到它-有一个类
org.springframework.boot.autoconfigure.web.BasicErrorController

它的方法
error
在发生错误时被调用,如下所示:

{
    "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/"
}
@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);
}
@RequestMapping
@应答器
公共响应性错误(HttpServletRequest){
映射体=getErrorAttributes(请求,
isIncludeStackTrace(请求,MediaType.ALL));
HttpStatus status=getStatus(请求);
返回新的响应(主体、状态);
}

您可以在mehod
getErrorAttributes
中深入了解地图是如何构建的

下面的课程将让您了解在发生错误时如何生成响应 org.springframework.boot.autoconfigure.web.DefaultErrorAttributes

参考:

此外,如果您想在发生错误时自定义响应,也可以使用以下方法 @ControllerAdvice和@ExceptionHandler

用于错误处理的另一个类
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

这很有帮助,感谢外部链接。然而,这里的两个答案对我同样有帮助,我只能接受其中一个。所以我接受先回答的答案。:)