Mapstruct 1.4.2.Final:NullValuePropertyMappingStrategy.SET_TO_DEFAULT 未按预期工作
Mapstruct 1.4.2.Final : NullValuePropertyMappingStrategy.SET_TO_DEFAULT not working as expected
我最近将我的 spring 引导版本从 2.3.0.RELEASE 更新到 2.4.4,并将 Map Struct 版本从 1.3.1.Final 更新到最新版本 1.4 .2.Final。使用的 lombok 版本 - 1.18.18
但是在 1.3.1 中工作良好的映射器之一在 1.4.2 中不再工作,情况如下。
POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
......
<properties>
<!-- used for configuration meta data processor, please keep in sync with
spring boot parent version -->
<spring-boot.version>2.4.4</spring-boot.version>
<java.version>11</java.version>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
.....
</properties>
<dependencies>
........
........
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
........
........
</dependencies>
<build>
<finalName>dras_mt</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
........
........
</annotationProcessorPaths>
</configuration>
</plugin>
......
......
</plugins>
<pluginManagement>
.....
</pluginManagement>
</build>
</project>
实体:
@Entity
@Data
public class TestEntity {
@Id
private Long id;
@NotNull
private String plntId;
@NotNull
private Boolean approvalNeeded = false;
}
域:
@Value
@Builder(toBuilder = true)
public class TestDomain {
private PlantDomain plantDomain;
@NonFinal
@Setter
private SomeInfo someInfo;
@NonFinal
@Setter
@Default
private Boolean approvalNeeded = false;
}
映射器:
@Mapper(componentModel = "spring", uses = { ActaulPlantMapper.class })
public interface TestEntityMapper {
@Mapping(target = "someInfo", ignore = true)
@Mapping(source = "plntId", target = "plantDomain")
@Mapping(target = "approvalNeeded", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT)
TestDomain fromEntity(TestEntity entity);
}
使用 MapStruct 1.3.1 生成的代码
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2021-03-24T11:33:38+0530",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.9.1 (AdoptOpenJDK)"
)
@Component
public class TestEntityMapperImpl implements TestEntityMapper {
@Autowired
private ActualPlantMapper actualPlantMapper;
@Override
public TestDomain fromEntity(WareHouseOptionEntity entity) {
if ( entity == null ) {
return null;
}
TestDomainBuilder testDomain = TestDomain.builder();
testDomain.plantDomain( actualPlantMapper.fromSapId( entity.getPlntId() ) );
if ( entity.getApprovalNeeded() != null ) {
testDomain.approvalNeeded( entity.getApprovalNeeded() );
}
else {
testDomain.approvalNeeded( false );
}
return testDomain.build();
}
}
使用 MapStruct 1.4.2 生成的代码
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2021-03-24T11:33:38+0530",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.9.1 (AdoptOpenJDK)"
)
@Component
public class TestEntityMapperImpl implements TestEntityMapper {
@Autowired
private ActualPlantMapper actualPlantMapper;
@Override
public TestDomain fromEntity(WareHouseOptionEntity entity) {
if ( entity == null ) {
return null;
}
TestDomainBuilder testDomain = TestDomain.builder();
testDomain.plantDomain( actualPlantMapper.fromSapId( entity.getPlntId() ) );
testDomain.approvalNeeded( entity.getApprovalNeeded() );
return testDomain.build();
}
}
正如您在 1.4.2 版本中看到的那样,NullValuePropertyMappingStrategy.SET_TO_DEFAULT 未应用。
我尝试过其他选项,例如将 NullValuePropertyMappingStrategy 置于 class 级别,但没有帮助。
非常感谢任何进一步的输入。
NullValuePropertyMappingStrategy
开发 1.3 纯属巧合。该策略应仅适用于更新方法,即具有 @MappingTarget
.
的方法
如果您想保留源代码中定义的默认值 class 我建议您使用 NullValueCheckStrategy#ALWAYS
我最近将我的 spring 引导版本从 2.3.0.RELEASE 更新到 2.4.4,并将 Map Struct 版本从 1.3.1.Final 更新到最新版本 1.4 .2.Final。使用的 lombok 版本 - 1.18.18
但是在 1.3.1 中工作良好的映射器之一在 1.4.2 中不再工作,情况如下。
POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
......
<properties>
<!-- used for configuration meta data processor, please keep in sync with
spring boot parent version -->
<spring-boot.version>2.4.4</spring-boot.version>
<java.version>11</java.version>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
.....
</properties>
<dependencies>
........
........
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
........
........
</dependencies>
<build>
<finalName>dras_mt</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
........
........
</annotationProcessorPaths>
</configuration>
</plugin>
......
......
</plugins>
<pluginManagement>
.....
</pluginManagement>
</build>
</project>
实体:
@Entity
@Data
public class TestEntity {
@Id
private Long id;
@NotNull
private String plntId;
@NotNull
private Boolean approvalNeeded = false;
}
域:
@Value
@Builder(toBuilder = true)
public class TestDomain {
private PlantDomain plantDomain;
@NonFinal
@Setter
private SomeInfo someInfo;
@NonFinal
@Setter
@Default
private Boolean approvalNeeded = false;
}
映射器:
@Mapper(componentModel = "spring", uses = { ActaulPlantMapper.class })
public interface TestEntityMapper {
@Mapping(target = "someInfo", ignore = true)
@Mapping(source = "plntId", target = "plantDomain")
@Mapping(target = "approvalNeeded", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT)
TestDomain fromEntity(TestEntity entity);
}
使用 MapStruct 1.3.1 生成的代码
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2021-03-24T11:33:38+0530",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.9.1 (AdoptOpenJDK)"
)
@Component
public class TestEntityMapperImpl implements TestEntityMapper {
@Autowired
private ActualPlantMapper actualPlantMapper;
@Override
public TestDomain fromEntity(WareHouseOptionEntity entity) {
if ( entity == null ) {
return null;
}
TestDomainBuilder testDomain = TestDomain.builder();
testDomain.plantDomain( actualPlantMapper.fromSapId( entity.getPlntId() ) );
if ( entity.getApprovalNeeded() != null ) {
testDomain.approvalNeeded( entity.getApprovalNeeded() );
}
else {
testDomain.approvalNeeded( false );
}
return testDomain.build();
}
}
使用 MapStruct 1.4.2 生成的代码
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2021-03-24T11:33:38+0530",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.9.1 (AdoptOpenJDK)"
)
@Component
public class TestEntityMapperImpl implements TestEntityMapper {
@Autowired
private ActualPlantMapper actualPlantMapper;
@Override
public TestDomain fromEntity(WareHouseOptionEntity entity) {
if ( entity == null ) {
return null;
}
TestDomainBuilder testDomain = TestDomain.builder();
testDomain.plantDomain( actualPlantMapper.fromSapId( entity.getPlntId() ) );
testDomain.approvalNeeded( entity.getApprovalNeeded() );
return testDomain.build();
}
}
正如您在 1.4.2 版本中看到的那样,NullValuePropertyMappingStrategy.SET_TO_DEFAULT 未应用。
我尝试过其他选项,例如将 NullValuePropertyMappingStrategy 置于 class 级别,但没有帮助。
非常感谢任何进一步的输入。
NullValuePropertyMappingStrategy
开发 1.3 纯属巧合。该策略应仅适用于更新方法,即具有 @MappingTarget
.
如果您想保留源代码中定义的默认值 class 我建议您使用 NullValueCheckStrategy#ALWAYS