Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 带有条件和nullValuePropertyMappingStrategy的Mapstruct映射_Spring Boot_Mapstruct - Fatal编程技术网

Spring boot 带有条件和nullValuePropertyMappingStrategy的Mapstruct映射

Spring boot 带有条件和nullValuePropertyMappingStrategy的Mapstruct映射,spring-boot,mapstruct,Spring Boot,Mapstruct,如果标题不清楚,我深表歉意,让我通过给出示例代码来说明: UpdateProfileDto public class UpdateProfileDto { @NotEmpty private String firstName; @NotEmpty private String lastName; @Size(max = 20) private String currentPassword; @Size(max = 20) p

如果标题不清楚,我深表歉意,让我通过给出示例代码来说明:

UpdateProfileDto

public class UpdateProfileDto {

    @NotEmpty
    private String firstName;

    @NotEmpty
    private String lastName;

    @Size(max = 20)
    private String currentPassword;

    @Size(max = 20)
    private String newPassword;

    @Size(max = 20)
    private String confirmNewPassword;

    // getters and setters
}
编码映射

@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface EncodedMapping {
}
PasswordEncoderMapper

public class PasswordEncoderMapper {
    protected final PasswordEncoder passwordEncoder;

    public PasswordEncoderMapper(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    @EncodedMapping
    public String encode(String value) {
        return passwordEncoder.encode(value);
    }
}
@Mapper(config = MapperConfig.class, componentModel = "spring", uses = PasswordEncoderMapper.class)
public interface UserMapper {

    @Mappings({
            @Mapping(target = "firstName", source = "firstName"),
            @Mapping(target = "lastName", source = "lastName"),
            @Mapping(target = "fullName", expression = "java(user.getFirstName() + \" \" + user.getLastName())"),
            @Mapping(target = "password",
                    source = "newPassword",
                    nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE,
                    qualifiedBy = EncodedMapping.class)
    })
    void updateUserFromDto(UpdateUserProfileDto updateUserProfileDto, @MappingTarget User user);
}
UserMapper

public class PasswordEncoderMapper {
    protected final PasswordEncoder passwordEncoder;

    public PasswordEncoderMapper(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    @EncodedMapping
    public String encode(String value) {
        return passwordEncoder.encode(value);
    }
}
@Mapper(config = MapperConfig.class, componentModel = "spring", uses = PasswordEncoderMapper.class)
public interface UserMapper {

    @Mappings({
            @Mapping(target = "firstName", source = "firstName"),
            @Mapping(target = "lastName", source = "lastName"),
            @Mapping(target = "fullName", expression = "java(user.getFirstName() + \" \" + user.getLastName())"),
            @Mapping(target = "password",
                    source = "newPassword",
                    nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE,
                    qualifiedBy = EncodedMapping.class)
    })
    void updateUserFromDto(UpdateUserProfileDto updateUserProfileDto, @MappingTarget User user);
}
生成的UserMapperImpl

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2020-03-11T13:51:34+0800",
    comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_231 (Oracle Corporation)"
)
@Component
public class UserMapperImpl implements UserMapper {

    @Autowired
    private PasswordEncoderMapper passwordEncoderMapper;

    @Override
    public void updateUserFromDto(UpdateUserProfileDto updateUserProfileDto, User user) {
        if ( updateUserProfileDto == null ) {
            return;
        }

        if ( updateUserProfileDto.getFirstName() != null ) {
            user.setFirstName( updateUserProfileDto.getFirstName() );
        }
        else {
            user.setFirstName( null );
        }
        if ( updateUserProfileDto.getLastName() != null ) {
            user.setLastName( updateUserProfileDto.getLastName() );
        }
        else {
            user.setLastName( null );
        }
        if ( updateUserProfileDto.getNewPassword() != null ) {
            user.setPassword( passwordEncoderMapper.encode( updateUserProfileDto.getNewPassword() ) );
        }

        user.setFullName( user.getFirstName() + " " + user.getLastName() );
    }
}
从生成的
UserMapperImpl
,我想检查的不仅仅是
newPassword
是否有值。。。但是要检查
currentPassword
newPassword
是否有值,请继续执行
user.setPassword()

我的意思是这样的:

...
if ( updateUserProfileDto.getCurrentPassword() != null && updateUserProfileDto.getNewPassword() != null ) {
    user.setPassword( passwordEncoderMapper.encode( updateUserProfileDto.getNewPassword() ) );
}
...
问题

如何更改映射器界面
UserMapper
,以便在设置目标
user.password
之前检查
currentPassword
newPassword
,并且仍然使用
PasswordEncoderMapper.encode(password)

如果我尝试使用
expression
而不是
source
,并检查
currentPassword
newPassword
是否都有值,然后将
user.password
设置为
newPassword
。否则,它将不会使用
NullValuePropertyMappingStrategy
user.password
执行任何操作。。。但似乎不允许混合使用
表达式
空值属性映射策略


谢谢

我将从以下方法开始

@Mapper(config=MapperConfig.class,componentModel=“spring”)
公共抽象类UserMapper{//使用类而不是接口来注入bean
@自动连线
私有密码编码器映射器密码编码器映射器;
@映射({
//您的非密码映射
})
void updateUserFromDto(UpdateUserProfileDto UpdateUserProfileDto,@MappingTarget User);
@后映射
void setPassword(UpdateUserProfileDto UpdateUserProfileDto,@MappingTarget User){
if(updateUserProfileDto.getCurrentPassword()!=null&&updateUserProfileDto.getNewPassword()!=null){
user.setPassword(passwordEncoderMapper.encode(updateUserProfileDto.getNewPassword());
}
}
}

还可以为新密码添加状态检查,以检查当前密码和新密码。这样,MapStruct将使用状态检查。