使用@RequiredArgsConstructor Lombok 的父类的子类在父类中找不到默认构造函数

问题描述

我正在使用 Lombok 插件进行 Spring 项目。我添加@requiredArgsConstructor 注释并使父类的字段 final 使用 @AllArgsConstructor@requiredArgsConstructor 而不是字段注入(使用 @Autowired 用于类字段)来初始化它. 但是,出于某种原因,子类 DraftsPostMethod 报告了一条消息:父类 AbstractContributoryServiceMethod

没有找到认构造函数

我不确定是什么原因造成的,因为我在其他类上做了同样的事情(添加了注释)来初始化它们 Lombok 方式。

子类报错“There is no default constructor available in ... AbstractContributoryServiceMethod

子类:

@Service
public class DraftsPostMethod extends AbstractContributoryServiceMethod<DraftManagementRequest,BasicServiceModel<DraftManagementRequest>,BasicServiceResponse<Void>> {
    
    private final Validator<DraftManagementRequest> validator = new Validator<DraftManagementRequest>(){
        @Nonnull
        @Override
        public ValidationState validate(DraftManagementRequest draftManagementRequest){
            return ValidationState.commence();
        }
    };
    
    private final DraftService draftService;
    
    @Autowired
    public DraftsPostMethod(DraftService draftService){ // getting the red line error here
        this.draftService = draftService;
    }
    //...and so on...
}

父类

@requiredArgsConstructor
public abstract class AbstractContributoryServiceMethod {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    private final ExceptionManager exceptionManager; //this line was @Autowired private ExceptionManager exceptionManager
    private final ValidationErrorTranslator validationErrirTranslator; //this line was @Autowired private ValidationErrorTranslator validationErrirTranslator
    private final ExceptionDetailsLogger exceptionDetailsLogger; //this line was @Autowired private ExceptionDetailsLogger exceptionDetailsLogger
    
    //... and so on...
}

如有任何意见或答案,我将不胜感激。谢谢。

解决方法

Lombok 的 @NoArgsConstructor 注释将生成它正在寻找的默认构造函数。

https://projectlombok.org/features/constructor