Java MapStruct nullValueMappingStrategy原语到bean返回默认bean而不是null值
MapStruct版本:1.4.1.Final 当我试图将一个整数映射到bean时,当整数为null时,目标仍然被创建为默认对象,而不是nullJava MapStruct nullValueMappingStrategy原语到bean返回默认bean而不是null值,java,spring-boot,mapstruct,Java,Spring Boot,Mapstruct,MapStruct版本:1.4.1.Final 当我试图将一个整数映射到bean时,当整数为null时,目标仍然被创建为默认对象,而不是null @Mapper(componentModel=“spring”,nullValueMappingStrategy=nullValueMappingStrategy.RETURN\u NULL,nullValuePropertyMappingStrategy=nullValuePropertyMappingStrategy.SET\u为\u NULL)
@Mapper(componentModel=“spring”,nullValueMappingStrategy=nullValueMappingStrategy.RETURN\u NULL,nullValuePropertyMappingStrategy=nullValuePropertyMappingStrategy.SET\u为\u NULL)
公共接口公司映射{//NOSONAR
CompanyMapper实例=Mappers.getMapper(CompanyMapper.class);
@映射(source=“parentId”,target=“parent.id”)
公司实体(请求公司请求);
}
生成的代码
@覆盖
上市公司实体(请求公司请求){
if(请求==null){
返回null;
}
CompanyBuilder company=company.builder();
公司。母公司(请求公司)(请求公司);
//为简单起见,删除了
返回company.build();
}
受保护公司请求公司(请求公司请求公司){
if(requestCompany==null){
返回null;
}
CompanyBuilder company=company.builder();
//应验证parentId是否为null,以及
//如果满足条件,则返回null
company.id(requestCompany.getParentId());
返回company.build();
}
编辑:与相关。此功能按预期工作。MapStruct无法知道源对象的哪些属性需要被视为执行映射的关键属性 为了实现你所寻找的,你必须为它提供你自己的映射方法 e、 g 注意:使用
spring
组件模型时,建议不要使用Mapper#getMapper
@Mapper(componentModel = "spring", nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL, nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_NULL)
public interface CompanyMapper { // NOSONAR
default Company toEntity(RequestCompany request) {
if (request == null || request.getParentId() == null) {
return null;
}
return toEntity2(request);
}
@Named("ignoreForOtherMethods")
@Mapping(source = "parentId", target = "parent.id")
Company toEntity2(RequestCompany request);
}