【问题标题】:Where does the default JSON errors response in spring-boot-starter-web comes from and how to adjust it?spring-boot-starter-web 中默认的 JSON 错误响应来自哪里,如何调整?
【发布时间】:2015-04-05 16:34:37
【问题描述】:

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

{
  "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"
}

感谢您的帮助, 马吕斯

【问题讨论】:

    标签: spring-mvc configuration spring-boot default


    【解决方案1】:

    输出由 Spring Boot 的 BasicErrorController 创建。当您的应用程序未使用 Spring MVC(例如 ExceptionHandlerControllerAdvice)或容器错误页面处理异常时,它会用作后备。

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

    有关详细信息,请参阅 Spring Boot 文档的 error handling section

    【讨论】:

    • 非常感谢安迪,这很有帮助:)
    猜你喜欢
    • 2015-05-20
    • 1970-01-01
    • 2017-11-10
    • 2017-08-08
    • 2015-08-30
    • 2014-12-29
    • 2021-10-08
    • 2016-01-29
    相关资源
    最近更新 更多