使用 SuperBuilder 使用 Lombok 扩展父级 class

Using SuperBuilder to extend parent class using Lombok

我有一个使用 Lombok 功能的 DTO,如图所示 below.But 现在由于某些要求,我不得不将我的 DTO 扩展到父级 class,看起来像 below.How 我可以吗在我的 DTO 中做最小的更改以支持 that.I 尝试使用 @SuperBuilder 注释但失败了。

DTO 之前:

@Getter
@ToString
@EqualsAndHashCode
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class RequestMessage {
private final String name;
 }

需要扩展的父级 Class

   @Data
   @SuperBuilder(toBuilder = true)
   @JsonDeserialize(builder = MyDTO.Builder.class)
   public abstract class MyDTO implements Serializable {
   @JsonIgnore private final ObjectMapper objectMapper = new ObjectMapper();
   protected String myAccountId;

   protected MyDTO() {}

   public static int hashCode(Object... objects) {
     return Arrays.deepHashCode(objects);
   }

   public static boolean equal(Object o1, Object o2) {
    // implementation of equals method
   }

   public abstract String emitSerializedPayload() throws JsonProcessingException;

   @JsonPOJOBuilder(withPrefix = "")
   protected abstract static class Builder<T extends MyDTO, B extends Builder<T, B>> {
   protected T dtoInstance;
   protected B builderInstance;

   public Builder() {
  dtoInstance = createDtoInstance();
  builderInstance = returnBuilderInstance();
}

protected abstract T createDtoInstance();

protected abstract B returnBuilderInstance();

public B myAccountId(String accountId) {
  dtoInstance.myAccountId = accountId;
  return builderInstance;
}

public T build() {
  return dtoInstance;
}
}
}

我尝试手动构建 RequestMessageClass,它工作正常,但我的应用程序中有很多 classes,我不想手动更改它们,如何更改我现有的 RequestMessage class 带有注释或一些最小的更改以使其正常工作。

这是我尝试过的方法,但在执行此操作时出现编译错误

    RequestMessage.Builder().name(myName).myAccountId(myAcId).build();
     
     

我试过的如下图:

 @Getter
 @ToString
 @EqualsAndHashCode(callSuper = true)
 @SuperBuilder(toBuilder = true)
 @AllArgsConstructor(access = AccessLevel.PRIVATE)
 public class RequestMessage extends MyDTO {
 private final String name;

 @Override
 public String emitSerializedPayload() throws JsonProcessingException {
// TODO Auto-generated method stub
return null;
}
} 
 

您不应将 lombok @Builder 与静态内部生成器 class 混合使用。如果可以去掉 Builder class,下一个代码应该可以工作。

RequestMessage:

@Getter
@ToString
@EqualsAndHashCode(callSuper = true)
@SuperBuilder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class RequestMessage extends MyDTO {
    private final String name;

    @Override
    public String emitSerializedPayload() throws JsonProcessingException {
    return null;
    }
    public RequestMessage(String myAccountId, String name) {
        super(myAccountId);
        this.name = name;
}
}

MyDTO:

@Data
@SuperBuilder(toBuilder = true)
public abstract class MyDTO implements Serializable {
    @JsonIgnore private final ObjectMapper objectMapper = new ObjectMapper();
    protected String myAccountId;

    protected MyDTO() {}

    public MyDTO(String myAccountId) {
       this.myAccountId = myAccountId;
    }

    public static int hashCode(Object... objects) {
        return Arrays.deepHashCode(objects);
    }

    public static boolean equal(Object o1, Object o2) {
        // implementation of equals method
        return false;
    }
    public abstract String emitSerializedPayload() throws JsonProcessingException;
}

测试:

@Test
void name() {
    String myName = "myName";
    String myAccountId = "myAccountId";
    var request = RequestMessage.builder().name(myName).myAccountId(myAccountId).build();
    System.out.println("request = " + request);
    RequestMessage requestMessage = new RequestMessage(myAccountId, myName);
}