自从有了lombok这个库,自己就再也没有手动重写过实体类的toString方法。常用到的几个注解也就是:@Builder、@Data、@ToString。找了下@ToString里面的属性,有exclude可以指定跳过不打印哪些字段。
先写一个实体类:
@Data
public class Student{
private String name;
private int age;
private String sex;
private String email;
private String address;
}
使用exclude之前:
@Test
public void parse(){
Student student = new Student();
student.setName("jack");
student.setAge(26);
student.setSex("male");
student.setAddress("浙江杭州");
student.setEmail("xy123zk@163.com");
System.out.println(student);
}
/**
* 打印结果:
*AccountTest.Student(name=jack, age=26, sex=female, email=xy123zk@163.com, address=浙江杭州)
*/
使用exclude之后:
@Data
@ToString(exclude = {"name","email"})
public class Student{
private String name;
private int age;
private String sex;
private String email;
private String address;
}
/*
* 打印结果:
* AccountTest.Student(age=26, sex=female, address=浙江杭州)
*/
网友评论