杰克逊忽略了外部库中超类的所有属性 [英] Jackson ignore all properties of superclass from external library
问题描述
我正在使用ORM进行开发,我扩展了一个基础orm类来创建表。
I am developing using an ORM where I extend a base orm class to create tables.
例如:
public class Person extends DbItem {
@JsonIgnore
private String index;
private String firstName;
private String lastName;
}
问题是,当我使用ObjectMapper进行序列化时,它会尝试序列化成员DbItem类。有没有简单的方法来防止这种情况?例如,有一个注释。
Problem is that when I use ObjectMapper to serialize, it tries to serialize the members of the DbItem class. Is there any simple way to prevent this? For example with an annotation.
我看了一个类似的问题杰克逊序列化:如何忽略超类属性但我希望它可以做得更简单,我不确定我是否可以这样做,因为我无法改变超类,因为它在外部库中。
I had a look at a similar problem Jackson serialization: how to ignore superclass properties but I was hoping it could be done simpler, and I'm not sure if I could do it as I can't change the superclass since it is in an external library.
推荐答案
您可以使用混合或 @JsonIgnoreProperties
出于此目的在这些示例中,基本ORM类和扩展名假定为:
You can use a Mix-in or @JsonIgnoreProperties
For the purposes of these examples, the base ORM class and extension are assumed to be:
public class DbItem {
public String dbPropertyA;
public String dbPropertyB;
}
和
public class Person extends DbItem {
public String index;
public String firstName;
public String lastName;
}
。
混合是杰克逊从对象本身理解的de /序列化指令的抽象。这是一种自定义第三方类的de / serialization的方法。为了定义混合,必须使用 ObjectMapper
创建并注册抽象类。
A Mix-in is an abstraction of the de/serialization instructions that Jackson understands from an object itself. It is a way to customize de/serialization of 3rd party classes. In order to define a Mix-in, an abstract class must be created and registered with the ObjectMapper
.
public abstract class PersonMixIn {
@JsonIgnore public String dbPropertyA;
@JsonIgnore public String dbPropertyB;
@JsonIgnore public String index;
}
注册混合
Registering the Mix-in
@Test
public void serializePersonWithMixIn() throws JsonProcessingException {
// set up test data including parent properties
Person person = makeFakePerson();
// register the mix in
ObjectMapper om = new ObjectMapper()
.addMixIn(Person.class, PersonMixIn.class);
// translate object to JSON string using Jackson
String json = om.writeValueAsString(person);
assertFalse(json.contains("dbPropertyA"));
assertFalse(json.contains("dbPropertyB"));
assertFalse(json.contains("index"));
System.out.println(json);
}
@JsonIgnoreProperties
如果你想避免创建一个类并配置 ObjectMapper
,那么 @JsonIgnoreProperties
可以使用注释。只需注释要序列化的类,并列出要排除的属性。
@JsonIgnoreProperties
If you want to avoid creating a class and configuring the ObjectMapper
, the @JsonIgnoreProperties
annotation can be utilized. Simply annotate the class you are serializing and list the properties to exclude.
@JsonIgnoreProperties({"index", "dbPropertyA", "dbPropertyB"})
public class Person extends DbItem {
public String index;
public String firstName;
public String lastName;
}
See It In Action
See It In Action
@Test
public void serializePersonWithIgnorePropertiesAnnotation() throws JsonProcessingException {
// set up test data including parent properties
Person person = makeFakePerson();
ObjectMapper om = new ObjectMapper();
// translate object to JSON string using Jackson
String json = om.writeValueAsString(person);
assertFalse(json.contains("dbPropertyA"));
assertFalse(json.contains("dbPropertyB"));
assertFalse(json.contains("index"));
System.out.println(json);
}
这篇关于杰克逊忽略了外部库中超类的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!