在Java中使用Jackson进行序列化期间,如何忽略类?
Jackson@JsonIgnoreType批注 可用于在序列化 过程中忽略 类 ,并且可以标记在序列化和反序列化 JSON对象时要忽略的类的所有属性 或字段 。
语法
@Target(value={ANNOTATION_TYPE,TYPE})
@Retention(value=RUNTIME)
public @interface JsonIgnoreType示例
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import java.io.*;
public class JsonIgnoreTypeTest {
public static void main(String args[]) throws IOException {
Employee emp = new Employee();
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
System.out.println(jsonString);
}
}
//员工阶层
class Employee { @JsonIgnoreType public static class Address {
public String firstLine = null;
public String secondLine= null;
public String thirdLine = null;
@Override
public String toString() {
return "Address{" +
"firstLine='" + firstLine+ '\'' +
", secondLine='" + secondLine+ '\'' +
", thirdLine='" + thirdLine + '\'' +
'}';
}
} // end of Address class
public long empId = 115;
public String empName = "Raja Ramesh";
public Address empAddress = new Address();
@Override
public String toString() {
return "Employee{" +
"empId=" + empId +
", empName='" + empName + '\'' +
", empAddress=" + empAddress +
'}';
}
}输出结果
{
"empId" : 115,
"empName" : "Raja Ramesh"
}热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短