为什么@Data 和@Builder 不能一起工作

Why @Data and @Builder doesnt work together

我有这个简单的class

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

重构后,我添加了 @Data@Builder,但是所有实例化都不再起作用了

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

如果我删除了 @Builder,那么它将正常工作, 为什么我不能同时使用 @Data@Builder

龙目岛的@Buildermust have@AllArgsConstructor为了工作

Adding also @AllArgsConstructor should do

在幕后,它使用 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.

完整的配置应该是:

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