我们在使用hutool进行对象转json的时候一般都采用的是JSONUtil.toJsonStr(obj),有时候需要将对象的某个字段忽略不展示出来,这时候只需要在需要忽略的字段上加上transient关键字即可,如下示例:
User实体类:
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {
private transient String code;
private String name;
}
测试main方法:
public class Test {
public static void main(String[] args) {
User user = User.builder().code("1001").name("test").build();
System.out.println(JSONUtil.toJsonStr(user));
}
}
执行结果:
{"name":"test"}