Jackson JSON仅转换所选字段和方法

huangapple go评论85阅读模式
英文:

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;
}

这里statePerson类中不存在,因此该字段不会被映射。

英文:

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

huangapple
  • 本文由 发表于 2020年8月19日 21:43:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63488348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定