上一篇中,我们在《有效学习》中提供了有效的3种方法。
相对于不同的顺序领域,在实际应用中,更可能有一些领域的化成。
很容易了解一下如何非常化现场、同类的先睹为快,作成这一节简单的事。
本篇内容基于Jackson 2.11.2版本,马上开始学习吧。
为注解@JsonIgnore解,可以加载该字段的序列化和反序列化。
public class ArticleIgnore {
private String title;
@JsonIgnore
private String summary;
private String content;
@JsonIgnore
private String author;
// 省略setter、getter方法
@Override
public String toString() {
return "ArticleIgnore [title=" + title + ", summary=" + summary + ", content=" + content + ", author=" + author
+ "]";
}
}
/**
* 为字段添加@JsonIgnore注解,可以忽略该字段的序列化和反序列化。
*
* @throws JsonProcessingException
*/
@Test
public void jsonIgnore() throws JsonProcessingException {
ArticleIgnore article = new ArticleIgnore();
article.setTitle("title");
article.setSummary("summary");
article.setContent("content");
article.setAuthor("author");
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(article));
String str = "{\"title\":\"title\",\"summary\":\"summary\",\"content\":\"content\",\"author\":\"author\"}";
ArticleIgnore newArticle = mapper.readValue(str, ArticleIgnore.class);
System.out.println(newArticle.toString());
}
执行结果:
{"title":"title","content":"content"}
ArticleIgnore [title=title, summary=null, content=content, author=null]
为类JsonIgnoreProperties解注,可以添加指定多个字段的序列化和反序列化。
@JsonIgnoreProperties({ "summary", "author" })
public class ArticleIgnoreProperties {
private String title;
private String summary;
private String content;
private String author;
// 省略getter、setter方法
@Override
public String toString() {
return "ArticleIgnoreProperties [title=" + title + ", summary=" + summary + ", content=" + content + ", author="
+ author + "]";
}
}
/** * 为类添加@JsonIgnoreProperties注解,忽略指定字段的序列化和反序列化。 * * @throws JsonProcessingException */ @Test public void jsonIgnoreProperties() throws JsonProcessingException { ArticleIgnoreProperties article = new ArticleIgnoreProperties(); article.setTitle("title"); article.setSummary("summary"); article.setContent("content"); article.setAuthor("author"); ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writeValueAsString(article)); String str = "{\"title\":\"title\",\"summary\":\"summary\",\"content\":\"content\",\"author\":\"author\"}"; ArticleIgnoreProperties newArticle = mapper.readValue(str, ArticleIgnoreProperties.class); System.out.println(newArticle.toString()); }
执行结果:
{"title":"title","content":"content"}
ArticleIgnoreProperties [title=title, summary=null, content=content, author=null]
JsonIgnore 作用于不同的领域,JsonIgnore Properties 作用于类的领域,然后都可以指定的作用。
另外,还有一个以 JsonIgnore 的注解,用于调用指定类型(类型、接口)的字段。
在指定的类型上,@JsonIgnoreType 注解顺序,可以添加该类型的字段进行化。
public class AnimalIgnoreType {
private String name;
private Date date;
private Address address;
@JsonIgnoreType
public static class Address {
private String city;
// 忽略getter、setter方法
@Override
public String toString() {
return "Address [city=" + city + "]";
}
}
// 忽略getter、setter方法
@Override
public String toString() {
return "AnimalIgnoreType [name=" + name + ", date=" + date + ", address=" + address + "]";
}
}
/**
* 如果需要忽略某个具体的类型(类、接口)的序列化,可以使用@JsonIgnoreType注解来实现。
*
* @throws JsonProcessingException
*/
@Test
public void ignoreType() throws JsonProcessingException {
AnimalIgnoreType animal = new AnimalIgnoreType();
animal.setName("sam");
animal.setDate(new Date());
AnimalIgnoreType.Address address = new AnimalIgnoreType.Address();
address.setCity("gz");
animal.setAddress(address);
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(animal));
String jsonString = "{\"name\":\"sam\",\"date\":1601714443779,\"address\":{\"city\":\"gz\"}}";
AnimalIgnoreType newAnimal = mapper.readValue(jsonString, AnimalIgnoreType.class);
System.out.println(newAnimal.toString());
}
执行结果:
{"name":"sam","date":1601726994454}
AnimalIgnoreType [name=sam, date=Sat Oct 03 16:40:43 CST 2020, address=null]
地址类型添加了加载的多注解,因此在序列化和反序列化时该类型的字段被加载了。
前面使用JsonIgnoreType注解,加载的类型是固定的。
Object Mapper 的 addMixIn,可以动态的将 J IgnoreType Note 方法用于应用其他数据类型。
public class AnimalIgnoreType { private String name; private Date date; private Address address; public static class Address { private String city; // 省略getter、setter方法 @Override public String toString() { return "Address [city=" + city + "]"; } } // 省略getter、setter方法 @Override public String toString() { return "AnimalIgnoreType [name=" + name + ", date=" + date + ", address=" + address + "]"; } }
首先,定义一个空的类,并添加JsonIgnoreType注解。
@JsonIgnoreType
public class IgnoreType {}
在序列化时调用ObjectMappers的addIn方法,将IgnoreType注解Mixson类。
下面的例子,我加载类的注解,添加到日期地址上,因此化时这个类的字段会被加载。
/** * 调用ObjectMapper的addMixIn方法,将@JsonIgnoreType注解应用于任意目标类. * * @throws JsonProcessingException */ @Test public void mixIn() throws JsonProcessingException { AnimalIgnoreType animal = new AnimalIgnoreType(); animal.setName("sam"); animal.setDate(new Date()); AnimalIgnoreType.Address address = new AnimalIgnoreType.Address(); address.setCity("gz"); animal.setAddress(address); ObjectMapper mapper = new ObjectMapper(); mapper.addMixIn(Date.class, IgnoreType.class); mapper.addMixIn(Address.class, IgnoreType.class); System.out.println(mapper.writeValueAsString(animal)); }
执行结果:
{"name":"sam"}
如果需要指定的字段,使用 Json 我不注解是最简单的方法。
如果需要采取阿拉伯方式,有机会获得更多的机会,可以使用 JsonIgnore 来解决问题,灵活地解决。
添加Mix,要在某个类型的类型的使用中,就可以指定一个类型了。Jack 为我们提供了另外一种,可以JsonIgnoreType 来解出某个类型,或者结合Object Mapper 的类型来方法动态寻找数据类型的字段。
https://www.baeldung.com/jackson-ignore-properties-on-serialization
联系客服