一:
添加 @JsonIgnore 注解
com.fasterxml.jackson.annotation.JsonIgnore
二:
文档中描述是字段上加注解,默认配置是开启@Transient注解
是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
我们在使用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"}