简体   繁体   中英

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.

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.

Yes .

It is right way. You can provide any method generated by lombok . It will check that method already exist and skip generation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM