使用jackson mixin类获取对象列表

时间:2018-06-12 13:36:48

标签: jackson mixins

我遇到了反序列化以下json的问题

{
      "GrpHdr": {      
 "MsgId": "Message-1",
        "CreDtTm": "2018-03-02T10:15:30+01:00[Europe/Paris]",
        "NbOfTxs": "1",
        "InitgPty": {
          "Nm": "Remitter"
        }
        },

  "PmtInf": [
    {
      "PmtInfId": "1"
    },
    {
      "PmtInfId": "2"
    }
  ]

}

我创建了一个MixIn类:

公共抽象类CustomerCreditTransferInitiationMixIn {     public PaymentInstructions paymentInstructions;

@JsonCreator
public CustomerCreditTransferInitiationMixIn(
        @JsonProperty("GrpHdr") GroupHeader GrpHdr,
        @JsonProperty("PmtInf") List<PaymentInstruction> PmtInf

        ) {

    this.paymentInstructions = PaymentInstructions.valueOf(PmtInf);

}

@JsonProperty("GrpHdr")
abstract GroupHeader getGroupHeader();

@JsonProperty("PmtInf")
abstract List<PaymentInstruction> getPaymentInstructions();

}

我在这种情况下反序列化组头没有问题。映射不同的名称。但在PmtInf案中我感到困惑。这是一个我想要反序列化为PaymentInstructions列表的列表。但PmtInf是一种付款方式。

我创建了一个测试:

@Test
public void JacksonMixinAnnotationTestJsonIsoFileFromTester() throws JsonProcessingException, Throwable {

    CustomerCreditTransferInitiation customerCreditTransferInitiation;

    String jsonFile = "testWithShortNames";

    InputStream inputStream = new ClassPathResource(jsonFile + ".json").getInputStream();

    ObjectMapper objectMapper = buildMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    objectMapper.registerModule(new JavaTimeModule());

    objectMapper.addMixIn(CustomerCreditTransferInitiation.class, CustomerCreditTransferInitiationMixIn.class);
    objectMapper.addMixIn(GroupHeader.class, GroupHeaderMixIn.class);
    objectMapper.addMixIn(PaymentInstruction.class, PaymentInstructionMixIn.class);
    objectMapper.addMixIn(PartyIdentification.class, PartyIdentificationMixIn.class);

    customerCreditTransferInitiation = objectMapper.readValue(inputStream, CustomerCreditTransferInitiation.class);


    //GroupHeader
    Assert.assertNotNull(customerCreditTransferInitiation.getGroupHeader());
    Assert.assertNotNull(customerCreditTransferInitiation.getGroupHeader().getMessageId());
    Assert.assertNotNull(customerCreditTransferInitiation.getGroupHeader().getCreationDateTime());
    Assert.assertNotNull(customerCreditTransferInitiation.getGroupHeader().getNumberOfTransactions());
    Assert.assertNotNull(customerCreditTransferInitiation.getGroupHeader().getInitiatingParty());
    Assert.assertNotNull(customerCreditTransferInitiation.getGroupHeader().getInitiatingParty().getName());

    //PaymentInstructions
    Assert.assertNotNull(customerCreditTransferInitiation.getPaymentInstructions());}

收到以下错误:

  

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:   无法识别的字段“PmtInfId”(类   com.seb.payment.iso.domain.PaymentInstruction),未标记为   可忽略的(19个已知属性:“paymentInformationId”,   “paymentMethod”,“created”,“paymentTypeInformation”,“controlSum”,   “debtorAgent”,“instructionForDebtorAgent”,“numberOfTransactions”,   “requestExecutionTime”,“debtorAccount”,“creditTransferTransactions”,   “debtorAgentAccount”,“batchBooking”,“poolingAdjustmentDate”,   “ultimateDebtor”,“chargeBearerType”,“debtor”,“chargeAccount”,   “chargeAccountAgent”])在[来源:UNKNOWN; line:-1,column:-1]   (通过参考链:   com.seb.payment.iso.domain.CustomerCreditTransferInitiation [ “PmtInf”] - &GT; com.seb.payment.iso.domain.PaymentInstruction [ “PmtInfId”])

1 个答案:

答案 0 :(得分:0)

在我们的例子中,我们在abstract iterable中实现了自己的反序列化器。 上: ObjectReader objectReader = ObjectMapperFactory.instance()。readerFor(this.itemClass);

MixedIn类丢失