到目前为止,我对 spring-boot 项目非常满意,但我想更深入地了解所有东西是如何粘合在一起的。使用 spring-boot-starter-web、spring-boot-starter-data-jpa 和 hatoas 我能够组装一个很好的工作 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 Boot 的 BasicErrorController
创建.当您的应用程序未使用 Spring MVC(例如,使用 ExceptionHandler
或 ControllerAdvice
)或容器错误页面处理异常时,它用作后备。
JSON 是由 ErrorAttributes
的实现创建的.默认情况下,Spring Boot 将使用 DefaultErrorAttributes
.您可以通过创建自己的 @Bean
来自定义它实现 ErrorAttributes
.
见 error handling section有关更多信息,请参阅 Spring Boot 文档。
关于spring-mvc - spring-boot-starter-web 中默认的 JSON 错误响应来自哪里以及如何调整它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28350445/