我正在尝试使用Mapstruct映射和Lombok Builder创建补丁方法。
但是在生成的代码中缺少build()
调用
具有删除空值的映射器:
@Mapper(
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE
)
public interface CartMapper {
CartMapper INSTANCE = Mappers.getMapper(CartMapper.class);
Cart cartRequestToCart(CartRequest cartRequest);
Cart patch(CartRequest cartRequest, @MappingTarget Cart cart);
}
嵌套类与购物车具有相同的lombok注释:
@Builder
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
@Setter
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Cart {
Customer customer;
Owner owner;
Integer price;
String voucher;
}
所有嵌套类都具有与CartRequest相同的lombok批注,并且结构与目标实体结构相同
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class CartRequest {
CustomerRequest customer;
OwnerRequest owner;
Integer price;
String voucher;
}
对于方法cartRequestToCart
,一切正常,在此处以及在嵌套方法中都调用了构建器
@Override
public Cart cartRequestToCart(CartRequest cartRequest) {
if ( cartRequest == null ) {
return null;
}
CartBuilder cart = Cart.builder();
if ( cartRequest.getCustomer() != null ) {
cart.customer( customerRequestToCustomer( cartRequest.getCustomer() ) );
}
if ( cartRequest.getOwner() != null ) {
cart.owner( ownerRequestToOwner( cartRequest.getOwner() ) );
}
if ( cartRequest.getPrice() != null ) {
cart.price( cartRequest.getPrice() );
}
if ( cartRequest.getVoucher() != null ) {
cart.voucher( cartRequest.getVoucher() );
}
return cart.build();
}
对于方法patch
的生成是这样的,这里有些设置方法缺少build()
调用
@Override
public Cart patch(CartRequest cartRequest, Cart cart) {
if ( cartRequest == null ) {
return null;
}
if ( cartRequest.getCustomer() != null ) {
if ( cart.getCustomer() == null ) {
cart.setCustomer( Customer.builder() ); // HERE build() missing
}
customerRequestToCustomer1( cartRequest.getCustomer(), cart.getCustomer() );
}
if ( cartRequest.getOwner() != null ) {
if ( cart.getOwner() == null ) {
cart.setOwner( Owner.builder() ); // HERE build() missing
}
ownerRequestToOwner1( cartRequest.getOwner(), cart.getOwner() );
}
if ( cartRequest.getPrice() != null ) {
cart.setPrice( cartRequest.getPrice() );
}
if ( cartRequest.getVoucher() != null ) {
cart.setVoucher( cartRequest.getVoucher() );
}
return cart;
}
一些提示如何解决此问题?非常感谢。
答案 0 :(得分:2)
龙目岛 1.18.16 的更新。
为 maven-compiler-plugin
启动此版本的工作配置是
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.1.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
path
部分的顺序很重要!
答案 1 :(得分:0)
这是MapStruct 1.3.0.Final中的错误。
我建议遵循mapstruct/mapstruct#1742进行修复。
当前的解决方案是通过SPI设置NoOpBuilderProvider
来完全禁用构建器。
答案 2 :(得分:0)
即使仅使用@Builder和@Getter,也请选中此worked for me
答案 3 :(得分:0)
正如Gregory Mazur指出的那样,this article也为我提供了帮助,但是我拥有Maven构建系统。这是Maven的配置版本:
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.1.Final</version>
</dependency>
...
</dependencies>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.1.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>