将嵌套对象的一个属性添加到toString方法中,使用Lombok。

huangapple go评论91阅读模式
英文:

Include only one property of nested object to toString method using Lombok

问题

我有以下情况:

  1. @Data
  2. public class Human {
  3. private long id;
  4. private Pet pet;
  5. //其他属性...
  6. }
  1. @Data
  2. public class Pet {
  3. private long id;
  4. private Human owner;
  5. //其他属性...
  6. }

我使用Lombok依赖项,当使用toString方法时,应用程序抛出java.lang.StackOverflowError异常。我知道我可以使用@ToString.Exclude来排除特定字段,但在这种情况下,我希望在toString方法中只显示嵌套对象的id属性。

换句话说,如果对象的toString方法调用其嵌套属性的toString方法,那么(仅在那种情况下)我希望只看到该对象的id。但如果我不是从父对象调用该对象的toString方法,我希望看到常规的toString

我知道我可以自己定义toStringequalshashCode方法来实现这一点,但是否可以告诉Lombok来完成这个任务?

英文:

i have a following case:

  1. @Data
  2. public class Human{
  3. private long id;
  4. private Pet pet;
  5. //other properties...
  6. }
  1. @Data
  2. public class Pet{
  3. private long id;
  4. private Human owner;
  5. //other properties...
  6. }

I use lombok dependency and when toString is used, application throws java.lang.StackOverflowError. I know I can use @ToString.Exclude to exclude given field, but in such case i would like to have only a nested object's id proeprty to be shown in a toString method.<br>
In other words, if an object's toString calls it's nested property toString, then (and only then) I want to see only that object id. But if i call that's object toString not from a parent object, I want to see a regular toString. <br>
I know that i can define toString, equals and hashCode methods by myself this way, but is this possible to tell lombok to do that?

答案1

得分: 2

一种方法是为宠物/主人的ID添加一个getter方法,并将其标记为@ToString.Include

  1. @Data
  2. class Human{
  3. private long id;
  4. @ToString.Exclude
  5. @EqualsAndHashCode.Exclude
  6. private Pet pet;
  7. @ToString.Include
  8. public long petId() { return pet.getId(); }
  9. //其他属性...
  10. }
  11. @Data
  12. class Pet{
  13. private long id;
  14. @ToString.Exclude
  15. @EqualsAndHashCode.Exclude
  16. private Human owner;
  17. @ToString.Include
  18. public long ownerId() { return owner.getId(); }
  19. }

用法:

  1. var human = new Human();
  2. var pet = new Pet();
  3. human.setPet(pet);
  4. pet.setOwner(human);
  5. System.out.println(human);
  6. // 输出:
  7. // Human(id=0, petId=0)

注意,您可能还希望将petowner标记为@EqualsAndHashCode.Exclude,因为否则在某些情况下,equalshashCode也可能导致堆栈溢出。

英文:

One way would be to add a getter for the pet/owner IDs, and mark that as @ToString.Include:

  1. @Data
  2. class Human{
  3. private long id;
  4. @ToString.Exclude
  5. @EqualsAndHashCode.Exclude
  6. private Pet pet;
  7. @ToString.Include
  8. public long petId() { return pet.getId(); }
  9. //other properties...
  10. }
  11. @Data
  12. class Pet{
  13. private long id;
  14. @ToString.Exclude
  15. @EqualsAndHashCode.Exclude
  16. private Human owner;
  17. @ToString.Include
  18. public long ownerId() { return owner.getId(); }
  19. }

Usage:

  1. var human = new Human();
  2. var pet = new Pet();
  3. human.setPet(pet);
  4. pet.setOwner(human);
  5. System.out.println(human);
  6. // Output:
  7. // Human(id=0, petId=0)

Note that you might also want to mark pet and owner as @EqualsAndHashCode.Exclude, because otherwise equals and hashCode might also cause stack overflows in some situations.

huangapple
  • 本文由 发表于 2023年2月14日 09:35:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定