FasterXML / FasterXML/jackson-modules-base

NoCtorDeser module can not deserialize class derived from Throwable without default constructor

未关闭
#342 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
no-ctor-deser
主要语言
Java
星标
180
派生
80
平均合并
3 小时 26 分钟
30 天内合并 PR
1

描述

JDK: 25.0.2+10
Jackson version: 2.21.2

When using a custom exception without a default constructor, the NoCtorDeser module will construct it with the cause field set to null which will then make Throwable.initCause() throw an exception when deserialization tries to set cause field to its actual value. The exception that is thrown is a bit misleading as the exception "Caused by: java.lang.IllegalStateException: Can't overwrite cause with a null" is lost as it isn't the root cause.

```java
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.module.noctordeser.NoCtorDeserModule;

public class ExceptionDemo {
public static class DemoException extends Exception {
public DemoException(String message, Throwable cause) {
super(message, cause);
}
}

public static void main(String[] args) throws Exception {
DemoException demoEx = new DemoException("Error message", null);
JsonMapper mapper = JsonMapper.builder()
.addModule(new NoCtorDeserModule())
.build();

byte[] bytes = mapper.writerFor(DemoException.class).writeValueAsBytes(demoEx);
DemoException decoded = mapper.readValue(bytes, DemoException.class);

System.out.println(decoded);
}
}
```

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。