Jackson parse json with unwraping root, but without ability to set @JsonRootName

前端 未结 1 1465
忘掉有多难
忘掉有多难 2020-11-30 12:08

The rest service responds with

1243654INVOICE


        
相关标签:
1条回答
  • 2020-11-30 12:41

    You can do it with mixin feature. You can create simple interface/abstract class like this:

    @JsonRootName("transaction")
    interface TransactionMixIn {
    
    }
    

    Now, you have to configure ObjectMapper object:

    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
    mapper.addMixInAnnotations(Transaction.class, TransactionMixIn.class);
    

    And finally you can use it to deserialize JSON:

    mapper.readValue(json, Transaction.class);
    

    Second option - you can write custom deserializer for Transaction class.

    0 讨论(0)
提交回复
热议问题