我有一个简单的类 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);
}
}
当我通过以下方式向ActiveMQ推送消息时。jmsTemplate.convertAndSend(destination, new B("foo", "bar"))
,队列中的实际消息如下。
{
"incomeField": "foo",
"anotherIncomeField": "bar",
"fixedValueField": null
}
我处理JMS消息的方法是 @JmsListener(destination = "my-destination)
并接收一个类型为 B
与 fixedValueField
设为 null
而我希望它是 someFixedValue
如我所愿 private
构造函数。
当我调试这些东西的时候,我发现 @JsonCreator
被正确调用,我的对象的所有字段都有预期的值,但当Jackson完成反序列化时,我发现 fixedValueField
是 null
.为什么会发生这种情况?
请注意,所有的字段都是 final
环境。
这就是为什么这样工作的答案 - stackoverflow.coma303411784760059
这可能是一个修复方法 - stackoverflow.coma421422684760059