@JsonIgnore注解的使用
<div class="article-info-box">
<div class="article-bar-top d-flex">
<span class="time">2017年07月14日 15:05:51</span>
<div class="float-right">
<span class="read-count">阅读数:2373</span>
</div>
</div>
</div>
<article>
<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
<div class="markdown_views">
<p><strong>一、jackson的maven依赖</strong></p>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
二、实体类的建立
import com.fasterxml.jackson.annotation.JsonIgnore;
public class Person {
private Integer id;
private String name;
@JsonIgnore
private String hobbies;//兴趣爱好
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobbies() {
return hobbies;
}
public void setHobbies(String hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", hobbies=" + hobbies + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
三、测试类代码:
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JSONTest {
public static void main(String[] args) throws IOException {
Person p = new Person();
p.setId(1001);
p.setName("huangbaokang");
p.setHobbies("篮球");
// 未加@JsonIgnore注解时,控制台打印输出{"id":1001,"name":"huangbaokang","hobbies":"篮球"}
// 加上@JsonIgnore注解时,控制台输出了{"id":1001,"name":"huangbaokang"},把hobbies的值过滤了。
System.out.println(new ObjectMapper().writeValueAsString(p));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15