java - 如何恢复 Spring Boot 1.5 中的默认异常映射?

标签 java spring spring-boot exception jersey

我有一个 Spring Boot Jersey 应用程序,我已将其从 Spring Boot 1.5.4(平台:Brussels-SR3)升级到 Boot 2.0.1(平台:Cairo-RELEASE),这将 Jersey 版本从 2.25.1 升级到 2.26

在升级之前,当应用程序抛出未处理的错误时,应用程序将返回如下响应:

{
    "timestamp": 1524060527236, "status": 500, "error": "Internal Server Error",
    "exception": "java.lang.IllegalStateException", "message": "<example message>",
    "path": "/ws/api/example/endpoint"
}

升级后相同的执行返回:

{
    "timestamp": "2018-04-24T17:00:06.151+0000", "status": 500, "error": "Internal Server Error",
    "message": "<example message>", "path": "/ws/api/example/endpoint"
}

我可以处理不同的时间戳格式,但丢失异常类是一个问题。

我尝试调试 Jersey 的异常映射,但我不熟悉代码库,而且它看起来相当复杂。我找不到映射发生在哪里。我在文档或发行说明中也找不到任何有关更改异常映射的提及。

根据上述内容和下面的评论,我认为更改是在 Spring 方面。

那么,发生了什么变化以及如何恢复旧的行为?

最佳答案

此错误响应不是来自 Jersey 。它实际上是在您将 spring-boot-starter-web 模块包含到项目中时生成的。基于Andy Wilkinson's comment ,我们可以通过创建 ErrorAttributes bean 轻松覆盖 JSON 属性。要添加异常,我们只需返回 DefaultErrorAttributes ,使用 constructor flag 创建它包括异常(exception)情况。

@Bean
public ErrorAttributes errorAttributes() {
    return new DefaultErrorAttributes(true);
}

我已经对此进行了测试,它包含预期的异常。

关于java - 如何恢复 Spring Boot 1.5 中的默认异常映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50007619/

相关文章:

gradle - 没有为属性 'mainClassName' 指定值

spring-boot - Spring Boot 2.6.6 的 Spring Cloud Kubernetes 负载均衡器问题

java - 有没有办法从Spring的任务:scheduled-tasks?获取元数据

java - Java 中 vector 不随 "AES/CBC/pkcs7padding"变化

java - Java 中的多重继承 - Spring Data

java - 初始化 Hibernate 中代理的所有属性

hibernate - Spring Boot 2/Kotlin/JDK9 - 由 : java. lang.ClassNotFoundException : javax. transaction.SystemException 引起

java - 未授权连接(五)——MQTT google cloud IOT

java - 如何通过sqoop从sybase导入数据?

java - Heroku 在日志中安装了错误的 jdk (8) 版本,但在终端中安装了良好的 jdk (11) 版本,这导致目标版本 : 11 无效