spring-boot-starter-web中的默认JSON错误响应来自何处以及如何调整它?

Mar*_*idt 9 configuration default spring-mvc spring-boot

到目前为止,我对弹簧启动项目非常满意,但我希望能够更深入地了解所有内容是如何粘合在一起的.使用spring-boot-starter-web,spring-boot-starter-data-jpa和hateoas,我能够组装一个漂亮的REST后端.但我想知道它是如何完成的,例如DataIntegrityViolation很好地转换为像这样的JSON输出.我实际上喜欢提供的信息,但我想知道,我如何重用DataObject转换为JSON.我只是不明白,它来自哪里以及配置的位置.希望大家可以帮助我或指出文档的相关部分甚至源代码.

{
  "readyState": 4,
  "responseText": "{\"timestamp\":1423155860435,\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"org.springframework.dao.InvalidDataAccessResourceUsageException\",\"message\":\"could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet\",\"path\":\"/api/catalog/colorfamilies\"}",
  "responseJSON": {
    "timestamp": 1423155860435,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.dao.InvalidDataAccessResourceUsageException",
    "message": "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet",
    "path": "/api/catalog/colorfamilies"
  },
  "status": 500,
  "statusText": "Internal Server Error"
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助,Marius

And*_*son 15

输出由Spring Boot创建BasicErrorController.当您的应用程序未使用Spring MVC(带有ExceptionHandler或者ControllerAdvice,例如)或容器错误页面处理异常时,它将用作后备.

JSON是由一个实现创建的ErrorAttributes.默认情况下,Spring Boot将使用DefaultErrorAttributes.您可以通过创建自己的@Bean实现来自定义它ErrorAttributes.

有关更多信息,请参阅Spring Boot文档的错误处理部分.