JAVA Lombok 使用@Data时,如何指定字段不加入到toString

题目描述

JAVA Lombok 使用@Data时,如何指定字段不加入到toString

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

import lombok.Data;
import java.io.Serializable;

@Data
public class User implements Serializable {

    private Integer id;

    private String name;

    private String code;

}

例如现在User类使用了@Data,当使用 new User().toString() 时,输出的字符串里面是有 id/name/code三个字段的,但如果我不想code字段在里面的话,我应该怎么做?

阅读 19.2k
3 个回答

排除掉@ToString(exclude="code")

By default, all non-static fields will be printed. If you want to skip some fields, you can annotate these fields with @ToString.Exclude

新手上路,请多包涵

直接在需要排除的字段上面注解 @ToString.Exclude

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题