Jackson 在调用@JsonCreator 后进行额外的初始化
Jackson makes extra initialization after @JsonCreator was called
我有一个简单的 class A
:
@Getter
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class A {
private final String incomeField;
private final String anotherIncomeField;
private final String fixedValueField;
}
class B extends A {
private B(final String incomeField, final String anotherIncomeField) {
super(incomeField, anotherIncomeField, "someFixedValue");
}
@JsonCreator
public static B of(@JsonProperty("incomeField") final String incomeField, @JsonProperty("anotherIncomeField") final String anotherIncomeField) {
return new B(incomeField, anotherIncomeField);
}
}
当我通过jmsTemplate.convertAndSend(destination, new B("foo", "bar"))
向ActiveMQ推送消息时,队列中的实际消息如下:
{
"incomeField": "foo",
"anotherIncomeField": "bar",
"fixedValueField": null
}
我使用 @JmsListener(destination = "my-destination)
处理 JMS 消息并接收类型为 B
的对象,其中 fixedValueField
设置为 null
,而我希望它是 someFixedValue
正如我在 private
构造函数中设置的那样。
当我调试所有这些东西时,我看到 @JsonCreator
被正确调用并且我的对象具有所有字段的预期值,但是当 Jackson 完成反序列化时,我看到 fixedValueField
是 null
。
为什么会发生这种情况?
注意所有字段都是final
环境:
- Java11
- Spring 启动 2.1.3.RELEASE
- 杰克逊 2.9.8
- ActiveMQ 5.15.8
这就是为什么它以这种方式工作的答案 - Whosebug。com/a/30341178/4760059
这可能是一个修复 - Whosebug。com/a/42142268/4760059
我有一个简单的 class A
:
@Getter
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class A {
private final String incomeField;
private final String anotherIncomeField;
private final String fixedValueField;
}
class B extends A {
private B(final String incomeField, final String anotherIncomeField) {
super(incomeField, anotherIncomeField, "someFixedValue");
}
@JsonCreator
public static B of(@JsonProperty("incomeField") final String incomeField, @JsonProperty("anotherIncomeField") final String anotherIncomeField) {
return new B(incomeField, anotherIncomeField);
}
}
当我通过jmsTemplate.convertAndSend(destination, new B("foo", "bar"))
向ActiveMQ推送消息时,队列中的实际消息如下:
{
"incomeField": "foo",
"anotherIncomeField": "bar",
"fixedValueField": null
}
我使用 @JmsListener(destination = "my-destination)
处理 JMS 消息并接收类型为 B
的对象,其中 fixedValueField
设置为 null
,而我希望它是 someFixedValue
正如我在 private
构造函数中设置的那样。
当我调试所有这些东西时,我看到 @JsonCreator
被正确调用并且我的对象具有所有字段的预期值,但是当 Jackson 完成反序列化时,我看到 fixedValueField
是 null
。
为什么会发生这种情况?
注意所有字段都是final
环境:
- Java11
- Spring 启动 2.1.3.RELEASE
- 杰克逊 2.9.8
- ActiveMQ 5.15.8
这就是为什么它以这种方式工作的答案 - Whosebug。com/a/30341178/4760059
这可能是一个修复 - Whosebug。com/a/42142268/4760059