繁体   English   中英

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

[英]How to make fasterxml ObjectMapper work with codehaus annotations

我正在使用fasterxml包( com.fasterxml.jackson.databind.ObjectMapper )中的ObjectMapper类来序列化一些POJO。

我面临的问题是 POJO 中的所有注释都来自较旧的 codehaus 库。 fasterxml ObjectMapper无法识别 codehaus jackson 注释。
一种可能的解决方案是将 POJO 中的注释更新为fasterxml ,但 POJO 由第三方提供,因此我无法对其进行修改。

我该如何解决这个问题?

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

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

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

如果您可以使用继承的解决方法

// 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 类,它将被正确序列化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM