Java Jackson在调用@JsonCreator后进行额外初始化
我有一个简单的类Java Jackson在调用@JsonCreator后进行额外初始化,java,spring-boot,jackson,jms,deserialization,Java,Spring Boot,Jackson,Jms,Deserialization,我有一个简单的类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 Str
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(目的地,新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
环境:
- 爪哇11
- 弹簧护套2.1.3.1释放
- 杰克逊2.9.8
- ActiveMQ 5.15.8
这是一个可能的修复-stackoverflow.com/a/42142268/4760059这就是为什么它会以这种方式工作的答案-stackoverflow.com/a/30341178/4760059 这可能是一个修复-stackoverflow.com/a/42142268/4760059