【问题标题】:Jackson makes extra initialization after @JsonCreator was called在调用@JsonCreator 之后,Jackson 进行了额外的初始化
【发布时间】:2020-05-27 03:54:25
【问题描述】:

我有一个简单的课程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 消息并接收fixedValueField 设置为nullB 类型的对象,而我希望它是someFixedValue,因为我在private 构造函数中设置它。

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

注意所有字段都是final

环境:

  • Java 11
  • Spring Boot 2.1.3.RELEASE
  • 杰克逊 2.9.8
  • ActiveMQ 5.15.8

【问题讨论】:

    标签: java spring-boot jackson jms deserialization


    【解决方案1】:

    这就是为什么以这种方式工作的答案 - stackoverflow.com/a/30341178/4760059

    这可能是一个修复 - stackoverflow.com/a/42142268/4760059

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      相关资源
      最近更新 更多