带龙目岛注释的继承会出错 [英] Inheritance with lombok annotation get errors

查看:66
本文介绍了带龙目岛注释的继承会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,lombok用于避免为类编写getter和setter.我有两个类 Child 扩展了 Parent :

  @Value@Builder@AllArgsConstructor@JsonIgnoreProperties(ignoreUnknown = true)公共班级家长{@Nonnull@JsonProperty("personId")私有最终String personId;@JsonProperty("personTag")私有最终String personTag;...} 

还有

  @Value@Builder@AllArgsConstructor@JsonIgnoreProperties(ignoreUnknown = true)公共班级孩子扩展父母{@Nonnull@JsonProperty("childId")私有最终String childId;...} 

但这似乎不起作用,因为 Parent 中没有可用的默认构造函数.我对lombok注释不熟悉.有什么好的方法可以扩展Base类并同时使用lombok批注吗?

解决方案

类层次结构+ lombok不能很好地工作,因为在您的 Child 类上完成的lombok操作无法实现对父母一无所知.

但是,您的特定问题似乎可以回答:

Parent 类的构造函数可以接收所有字段,因为您要求lombok通过 @AllArgsConstructor 来构造此构造函数.因此,它没有no-args构造函数.如果要使用两个构造函数(一个使用所有字段的构造函数,另一个使用不带参数的第二个构造函数,又名默认构造函数),则还要添加一个 @NoArgsConstructor 批注,以告诉lombok您想要这样做.

注意: @Builder 也不适用于层次结构,但是全新的 @SuperBuilder 功能可以.我很确定您要在此处将 @Builder 替换为 @SuperBuilder .SuperBuilder要求层次结构中的所有类都用 @SuperBuilder 而不是 @Builder 进行注释.

In my project, lombok is used to avoid writing getters and setters for a class. I have two classes Child extends Parent:

@Value
@Builder
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Parent {
    @Nonnull
    @JsonProperty("personId")
    private final String personId;

    @JsonProperty("personTag")
    private final String personTag;
    ...
}

And

@Value
@Builder
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Child extends Parent {
    @Nonnull
    @JsonProperty("childId")
    private final String childId;
    ...
}

But this doesn't seems work as no default constructor available in Parent. I'm not familiar with the lombok annotation. Is there any good way to extend the Base class and use the lombok annotation at the same time?

解决方案

Class hierarchies + lombok doesn't work particularly well, in the sense that the lombok operations done on your Child class don't know anything about parent.

However, your specific question seems answerable:

The Parent class has a constructor that takes all fields, because you asked lombok to make this constructor via @AllArgsConstructor. Therefore, it does not have a no-args constructor. If you want both constructors (the one that takes all fields + a second one that takes no arguments, a.k.a. the default constructor), also add a @NoArgsConstructor annotation to tell lombok that you want that.

NB: @Builder does not work with hierarchy either, but the fresh new @SuperBuilder feature does. I'm pretty sure you want to replace @Builder with @SuperBuilder here. SuperBuilder requires that ALL classes in the hierarchy are annotated with @SuperBuilder and not @Builder.

这篇关于带龙目岛注释的继承会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆