java Jackson 忽略外部库中超类的所有属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29630371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson ignore all properties of superclass from external library
提问by simernes
I am developing using an ORM where I extend a base orm class to create tables.
我正在使用 ORM 进行开发,我扩展了一个基本的 orm 类来创建表。
For example:
例如:
public class Person extends DbItem {
@JsonIgnore
private String index;
private String firstName;
private String lastName;
}
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.
问题是当我使用 ObjectMapper 进行序列化时,它会尝试序列化 DbItem 类的成员。有什么简单的方法可以防止这种情况发生吗?例如带有注释。
I had a look at a similar problem Hymanson serialization: how to ignore superclass propertiesbut 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.
我看了一个类似的问题Hymanson 序列化:如何忽略超类属性,但我希望它可以做得更简单,我不确定我是否可以做到,因为我无法更改超类,因为它位于外部图书馆。
回答by Sam Berry
You can use a Mix-inor @JsonIgnoreProperties
您可以使用Mix-in或@JsonIgnoreProperties
For the purposes of these examples, the base ORM class and extension are assumed to be:
出于这些示例的目的,基本 ORM 类和扩展被假定为:
public class DbItem {
public String dbPropertyA;
public String dbPropertyB;
}
and
和
public class Person extends DbItem {
public String index;
public String firstName;
public String lastName;
}
respectively.
分别。
Using a Mix-in
使用混入
A Mix-in is an abstraction of the de/serialization instructions that Hymanson 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
.
Mix-in 是 Hymanson 从对象本身理解的反序列化指令的抽象。这是一种自定义第 3 方类的反/序列化的方法。为了定义一个 Mix-in,一个抽象类必须被创建并注册到ObjectMapper
.
Example Mix-in Definition
示例混合定义
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 Hymanson
String json = om.writeValueAsString(person);
assertFalse(json.contains("dbPropertyA"));
assertFalse(json.contains("dbPropertyB"));
assertFalse(json.contains("index"));
System.out.println(json);
}
@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.
如果要避免创建类和配置ObjectMapper
,@JsonIgnoreProperties
可以使用注释。只需注释您正在序列化的类并列出要排除的属性。
Example Serializable Object
示例序列化对象
@JsonIgnoreProperties({"index", "dbPropertyA", "dbPropertyB"})
public class Person extends DbItem {
public String index;
public String firstName;
public String lastName;
}
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 Hymanson
String json = om.writeValueAsString(person);
assertFalse(json.contains("dbPropertyA"));
assertFalse(json.contains("dbPropertyB"));
assertFalse(json.contains("index"));
System.out.println(json);
}
回答by Matthew Herbst
You want to do custom field level serialization. This will be a bit more work to maintain your code base, but is by far the simplest solution. See Hymanson JSON custom serialization for certain fieldsfor implementation details.
您想要进行自定义字段级序列化。这将需要更多的工作来维护您的代码库,但这是迄今为止最简单的解决方案。有关实现的详细信息,请参阅特定字段的 Hymanson JSON 自定义序列化。