Configuring lombok for builder

Peter Penzov :

I want to avoid multiple constructors, so I want to use a builder design pattern, by using lombok library, it can be more easier, so I want to annotate class of ContractDTO with this library annotation:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
class ContractDTO {

    private Integer id;  
    private String name;
    private Integer acquirerId;    
    private Integer terminalId;    
    private String merchantId;

}

then your code can be :

...
.map(g -> new ContractDTO().toBuilder()
        .name(g.getName())
        .merchantName(g.getMerchantId())
        .build()
)....

But when I try to compile the code I get cannot find symbol [ERROR] symbol: method toBuilder()

Probably I need to generate the code in advance?

Anurag Dwivedi :

You can use it like this:

 ContractDTO.builder()
    .name(g.getName())
    .merchantName(g.getMerchantId())
    .build();

If we want to create copies or near-copies of objects, we can add the property toBuilder = true to the @Builder annotation. This tells Lombok to add a toBuilder() method to our Class. When we invoke the toBuilder() method, it returns a builder initialized with the properties of the instance it is called on.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=150581&siteId=1