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

标签 spring-mvc configuration spring-boot default

到目前为止,我对 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(例如,使用 ExceptionHandlerControllerAdvice)或容器错误页面处理异常时,它用作后备。

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/

相关文章:

spring - @RequestMapping 中前斜杠的处理

java - 使用@PropertySource配置Spring属性

java - Maven Spring 启动 :run against compiled jar

java - 基于类型参数 Autowiring CrudRepository

java - 如何使用 java Spring MVC 安排邮件

templates - thymeleaf :text - Put a text without removing HTML structures

java - 如何在我的 Controller 中使用 Spring HttpRequest?

java - 如何在运行时更改 log4j2 的配置文件?

java - 按方法级别@Order 注释对 spring @Beans 进行排序

java - 如何在 @Scheduled 注释中指定 PST 时区,该注释应在太平洋标准时间上午 7 点到下午 6 点之间每小时触发一次?