Melad Basilius
Melad Basilius

Reputation: 4306

Why @Data and @Builder doesnt work together

I have this simple class

public class ErrorDetails {
    private String param = null;
    private String moreInfo = null;
    private String reason = null;
     ...
}

After refactoring, I added @Data and @Builder, but all the instantiations doesn't work any more

ErrorDetails errorDetails = new ErrorDetails();

'ErrorDetails(java.lang.String, java.lang.String, java.lang.String)' is not public in 'com.nordea.openbanking.payments.common.ndf.client.model.error.ErrorDetails'. Cannot be accessed from outside package

If I removed @Builder, then it will work fine, Why I cannot use @Data and @Builder together?

Upvotes: 2

Views: 3061

Answers (2)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

The full config should be :

@Data
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
class ErrorDetails {
    private String param; // no need to initiate with null
    private String moreInfo;
    private String reason;
}

Upvotes: 3

Ori Marko
Ori Marko

Reputation: 58882

Lombok's @Builder must have @AllArgsConstructor in order to work

Adding also @AllArgsConstructor should do

Under the hood it build all fields using constructor with all fields

applying @Builder to a class is as if you added @AllArgsConstructor(access = AccessLevel.PACKAGE) to the class and applied the @Builder annotation to this all-args-constructor. This only works if you haven't written any explicit constructors yourself.

Upvotes: 5

Related Questions