javatostringlombokintellij-lombok-plugin

Exclude toString method generation that comes with Lombok @Data


I have a class that is annotated with @Data. But I want to exclude the toString() method and provide a custom toString.

I just defined a custom toString() method as I usually do if I was not using lombok and it seemed to work. Following is my example.

@Data
class SomeDTO {
    private String property1;
    private String property2;

    private String someReallyHugeString;

    @Override
    public String toString(){
        return "someReallyHugeString size is: " + someReallyHugeString.length() 
                  + "property1 = " + property1 
                  + "property2 = " + property2;
    }

}

But wanted to know if this is the right way to exclude toString() from @Data and if there are any side effects I am missing.


Solution

  • Just don't use @Data (but provide all the other annotations) that is has:

     @Getter
     @Setter
     @RequiredArgsConstructor
     @EqualsAndHashCode
     SomeDTO { .... 
         public String toString(){....}
     }
    

    This way if you remove toString by accident, it will not be generated.