问题描述
我对迄今为止的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\:\内部服务器错误\,\exception\:\org.springframework.dao.InvalidDataAccessResourceUsageException\,\message\:\无法提取ResultSet; SQL [n / a];嵌套异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet\,\path \:\/ api / catalog / colorfamilies\},
responseJSON: {
timestamp:1423155860435,
status:500,
error:Internal Server Error,
exception:org.springframework.dao。 InvalidDataAccessResourceUsageException,
message:无法提取ResultSet; SQL [n / a];嵌套异常为org.hibernate.exception.SQLGrammarException:无法提取ResultSet,
path: / api / catalog / colorfamilies
},
status:500,
statusText:内部服务器错误
}
Thx为您提供帮助,
Marius解决方案输出是由Spring Boot的
BasicErrorController
创建的。当你的应用程序没有使用Spring MVC(使用ExceptionHandler
或ControllerAdvice
)处理异常时,例如)或容器错误页面。
JSON由
ErrorAttributes
的实现创建。默认情况下,Spring Boot将使用DefaultErrorAttributes
。您可以通过创建自己的实现ErrorAttributes
的@Bean
来自定义。
请参见错误处理部分有关详细信息,请参阅Spring Boot文档。
I am very happy with the spring-boot project so far, but I'd like to develop a deeper understanding, of how everything is glued together. Using spring-boot-starter-web, spring-boot-starter-data-jpa and hateoas I was able to assemble a nice working REST backend. But I am wondering, how it is done, that e.g. a DataIntegrityViolation is converted nicely into a JSON output like this. I actually like the info being provided, but I wonder, how I could reuse the DataObject being converted to JSON. I just do not understand, where it comes from and where it is configure. Hope you folks can help me out or point me to the relevant parts of documentation or even source code.
{ "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" }
Thx for your help,Marius
解决方案The output is created by Spring Boot's
BasicErrorController
. It's used as a fallback when your application hasn't handled an exception using Spring MVC (with anExceptionHandler
orControllerAdvice
, for example) or a container error page.The JSON is created by an implementation of
ErrorAttributes
. By default, Spring Boot will useDefaultErrorAttributes
. You can customise this by creating your own@Bean
that implementsErrorAttributes
.See the error handling section of the Spring Boot documentation for more information.
这篇关于spring-boot-starter-web中的默认JSON错误响应在哪里来自以及如何调整它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-12 17:39