Last active
August 10, 2024 17:55
-
-
Save susimsek/bf72960b8ca4b94134fcf10e05834c14 to your computer and use it in GitHub Desktop.
Spring Boot Mapstruct With Password Encoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Data | |
| @NoArgsConstructor | |
| @AllArgsConstructor | |
| @Builder | |
| @FieldDefaults(level = AccessLevel.PRIVATE) | |
| public class CreateUserRequestModel { | |
| @NotBlank | |
| @Size(min=2) | |
| String firstName; | |
| @Size(min=2) | |
| String lastName; | |
| @NotBlank | |
| @Size(min=2,max = 16) | |
| String password; | |
| @NotBlank | |
| String email; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Qualifier // org.mapstruct.Qualifier | |
| @Target({ElementType.TYPE,ElementType.METHOD}) | |
| @Retention(RetentionPolicy.CLASS) | |
| public @interface EncodedMapping { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Configuration | |
| public class PasswordEncoderConfig { | |
| @Bean | |
| public PasswordEncoder passwordEncoder(){ | |
| return new BCryptPasswordEncoder(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component | |
| @RequiredArgsConstructor | |
| @FieldDefaults(level = AccessLevel.PRIVATE) | |
| public class PasswordEncoderMapper { | |
| final PasswordEncoder passwordEncoder; | |
| @EncodedMapping | |
| public String encode(String value) { | |
| return passwordEncoder.encode(value); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Data | |
| @NoArgsConstructor | |
| @AllArgsConstructor | |
| @Builder | |
| @FieldDefaults(level = AccessLevel.PRIVATE) | |
| public class UserDto implements Serializable { | |
| String userId; | |
| String firstName; | |
| String lastName; | |
| String password; | |
| String email; | |
| String encryptedPassword; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Mapper(uses = PasswordEncoderMapper.class) | |
| public interface UserMapper { | |
| @Mapping(source = "password", target = "encryptedPassword", qualifiedBy = EncodedMapping.class) | |
| UserDto createUserRequestModelToUserDto(CreateUserRequestModel userRequestModel); | |
| } |
thx for sharing <3 👍
This happen with you too?
I think it’s no longer relevant, but I’ll still write) the argument source is the model field, the argument target is the dto field. There you write your field names. If the names match, then just leave the target.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, first of all, you did a great work .
I did exactly the same, but i´m getting this error on my mapper class:
@Mapping(source = "password", target = "encryptedPassword", qualifiedBy = EncodedMapping.class) java: Unknown property "encryptedPassword" in result type User. Did you mean "password"?This happen with you too?