如何使用 lombok 打印主键标识符而不是对象

问题描述 投票:0回答:1

如何使用Lombok.toString打印

UserRel (from.getId(), rt.toString(), to.getId())
,我不想在log.trace方法中打印整个对象,而是打印主键标识符,有没有办法覆盖并实现我正在寻找的

@Slf4j
public class UserRel {

    @JsonProperty("from")
    @ToString.Exclude
    private User from;

    @JsonProperty("rt")
    private Rtype rtype;

    @JsonProperty("to")
    @ToString.Exclude
    private User to;
}
lombok
1个回答
0
投票

您有几个选择:

选项 1:

@ToString.Exclude
放在除
User
班级 id 之外的每个字段上。例如:

@Data
public class User {
    private Long id;
    @ToString.Exclude
    private String name;
    @ToString.Exclude
    private String email;
}

选项 2: 使用

onlyExplicitlyIncluded
修饰符表示
@ToString
,并仅包含 id。

@Data
@ToString(onlyExplicitlyIncluded = true)
public class User {
    @ToString.Include
    private Long id;
    private String name;
    private String email;
}

选项 3: 根据需要覆盖

toString()
,无需 Lombok。

@Data
public class UserRel {
    private User from;
    private User to;
    private String type;
    @Override
    public String toString() {
        return "UserRel{from=%d, to=%d, type='%s'}".formatted(from.getId(), to.getId(), type);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.