在调用@JsonCreator后,Jackson进行了额外的初始化。

问题描述 投票:1回答:1

我有一个简单的类 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) 并接收一个类型为 BfixedValueField 设为 null而我希望它是 someFixedValue 如我所愿 private 构造函数。

当我调试这些东西的时候,我发现 @JsonCreator 被正确调用,我的对象的所有字段都有预期的值,但当Jackson完成反序列化时,我发现 fixedValueFieldnull.为什么会发生这种情况?

请注意,所有的字段都是 final

环境。

  • 环境:Java 11
  • Spring Boot 2.1.3.RELEASE
  • 我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊
  • ActiveMQ 5.15.8
java spring-boot jackson jms deserialization
1个回答
0
投票

这就是为什么这样工作的答案 - stackoverflow.coma303411784760059

这可能是一个修复方法 - stackoverflow.coma421422684760059

© www.soinside.com 2019 - 2024. All rights reserved.