英文:
Jackson json only convert selected fields and methods
问题
使用@JsonIgnore
,Jackson库提供了一种忽略某些字段的方法。是否有一种方法来做相反的操作,只显示带有注解的字段呢?我正在使用一个带有大量字段的外部类,并且我只想选择其中的一小部分字段。我遇到了大量递归问题(使用某种类型的ORM),其中对象A -> B -> A -> B -> A...,这些问题甚至不必要进行导出。
英文:
With jackson there is a way to ignore some fields using @JsonIgnore
. Is there a way to do the opposite, and only show fields with are annotated? I'm working with an external class with a lot of fields and I only want to select a small subset of them. I'm getting tons of recursion problems (using some type of ORM) where object A -> B -> A -> B -> A .... which are not even necessary to export.
答案1
得分: 1
当然可以;创建一个仅包含所需字段的类,并在对象映射器中添加以下属性,其余部分就完成了。
将 DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES 设置为 false。
英文:
Yes definitely you can; Create a class with only the feilds you need and add the below property in the object mapper and rest is done.
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES to false
答案2
得分: 1
你可以配置对象映射器,除非由 JsonProperty
指定,否则它会忽略绝对所有内容。
public class JacksonConfig {
public static ObjectMapper getObjectMapper(){
//The marshaller
ObjectMapper marshaller = new ObjectMapper();
//Make it ignore all fields unless we specify them
marshaller.setVisibility(
new VisibilityChecker.Std(
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE
)
);
//Allow empty objects
marshaller.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false );
return marshaller;
}
}
public class MyObject {
private int id;
@JsonProperty
private String name;
private Date date;
//Getters Setters omitted
在这种情况下,只会序列化 name
。
示例仓库,https://github.com/DarrenForsythe/jackson-ignore-everything
英文:
You can configure the object mapper to ignore absolutely everything unless specified by JsonProperty
,
public class JacksonConfig {
public static ObjectMapper getObjectMapper(){
//The marshaller
ObjectMapper marshaller = new ObjectMapper();
//Make it ignore all fields unless we specify them
marshaller.setVisibility(
new VisibilityChecker.Std(
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE,
JsonAutoDetect.Visibility.NONE
)
);
//Allow empty objects
marshaller.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false );
return marshaller;
}
}
public class MyObject {
private int id;
@JsonProperty
private String name;
private Date date;
//Getters Setters omitted
in this case only name
would be serialized.
Sample repo, https://github.com/DarrenForsythe/jackson-ignore-everything
答案3
得分: 1
你可以在POJO类上使用 @JsonIgnoreProperties(ignoreUnknown=true),这样只会映射POJO类中可用的字段,而state
将被排除。
例如
JSON数据
{
"name":"Abhishek",
"age":30,
"city":"Banglore",
"state":"Karnatak"
}
POJO类
@JsonIgnoreProperties(ignoreUnknown=true)
Class Person{
private int id;
private String name;
private String city;
}
这里state
在Person
类中不存在,因此该字段不会被映射。
英文:
You can use @JsonIgnoreProperties(ignoreUnknown=true) on the pojo class so only the fields which are available in the pojo class will be mapped and resf will be left out.
For example
Json data
{
"name":"Abhishek",
"age":30,
"city":"Banglore",
"state":"Karnatak"
}
pojo class
@JsonIgnoreProperties(ignoreUnknown=true)
Class Person{
private int id;
private String name;
private String city;
}
Here state in not present in the Person class so that field won't be mapped
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论