如何使quickxml ObjectMapper与codehaus注释一起使用

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

我正在使用来自fasterxml包(com.fasterxml.jackson.databind.ObjectMapper)的ObjectMapper类来序列化一些POJO。我面临的问题是POJO中的所有注释都来自旧的codehaus库。 fastxml ObjectMapper无法识别codehaus jackson注释。一种可能的解决方案是将POJO中的注释更新为fasterxml,但POJO由第三方提供,因此我无法对其进行修改。请提出解决此问题的方法。

java json jackson
2个回答
2
投票

您可以提供自己的AnnotationIntrospector来处理旧注释。

ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new MyAnnotationIntrospector());

您还可以查看jackson github上列出的jackson-legacy-introspector。它是旧注释的AnnotationIntrospector的现有实现。


0
投票

如果您可以使用继承的变通方法

// Original class doesn't need to be modified
class Customer {
     @org.codehaus.jackson.annotate.JsonProperty("first_name")
     String firstName;
}

class CustomerWrapper extends Customer {
     @com.fasterxml.jackson.annotation.JsonProperty("first_name")
     String firstName;
}

在代码中使用CustomerWrapper类,它将被正确序列化

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