英文:
Is it possible to map with Mapstruct an attribute of object B to attribute of object A_DTO, when object A contains a reference to object B?
问题
According to Mapstruct documentation, it is possible to map an object (object A) that contains another object (object B) by defining a mapping method for the referenced object (object B). However, if you need to map only an attribute of that object (object B) and not the whole object, here's how you can do it with Java and Mapstruct:
@Mapper
public interface AppUserMapper {
AppUserMapper INSTANCE = Mappers.getMapper(AppUserMapper.class);
@Mapping(target = "authorities", source = "roles", qualifiedByName = "mapToAuthorities")
AppUserDetailsDTO toAppUserDetailsDTO(AppUserEntity appUserEntity);
@Named("mapToAuthorities")
default Collection<GrantedAuthority> mapToAuthorities(Set<RoleEntity> roles) {
return roles.stream()
.map(role -> new SimpleGrantedAuthority(role.getName()))
.collect(Collectors.toList());
}
}
In the code above, we define a custom mapping method called "mapToAuthorities" and use it to map the "roles" attribute of "AppUserEntity" to the "authorities" attribute of "AppUserDetailsDTO." This method converts the "RoleEntity" objects into "GrantedAuthority" objects, effectively mapping only the "name" attribute.
Make sure to use the @Mapper
annotation from Mapstruct to generate the implementation of this mapping interface.
Please note that you should adjust the code to match your project's package structure and dependencies.
英文:
According to Mapstruct documentation it is possible to map to DTO an object (object A) that contains another object (object B) by defining a mapping method for the referenced object (object B). But what if I need to map only attribute of that object (object B) and not the whole object?
Describing problem -
I am studing the Spring Boot and here is my project - https://github.com/Alex1182-St/java-spring-jpa-postgresql
With purpose of security I need to map my AppUserEntity to AppUserDetailsDTO (implements UserDetails) and especially I need to map the name from the attribute private Set<RoleEntity> roles
of my AppUserEntity to the private Collection<GrantedAuthority> authorities
of my AppUserDetailsDTO
With Kotlin it is easy (authorities = roles.map { it.name }):
fun AppUserEntity.toAppUserDetailsDTO() = AppUserDetailsDTO(
id = id,
username = appUserLogin,
password = appUserPassword,
authorities = roles.map { it.name },
isEnabled = isEnabled,
isAccountNonLocked = isAccountNonLocked,
isAccountNonExpired = isAccountNonExpired,
isCredentialsNonExpired = isCredentialsNonExpired
)
But How to do it with Java and Mapstruct?
答案1
得分: 2
On Mapstruct a method can be used for the mapping on the annotations using the expression property on the annotation: expression = "java( yourJavaCodeHere )"
.
Your mapper would look like:
@Mapper(componentModel = "spring")
public abstract class AppUserDetailsDtoMapper {
@Mappings({
@Mapping(target = "username", source = "appUserLogin"),
@Mapping(target = "password", source = "appUserPassword"),
@Mapping(target = "authorities", expression = "java( mapAuthorities(user.getRoles()) )")
})
public abstract AppUserDetailsDTO toAppUserDetailsDTO(AppUserEntity user);
protected Collection<GrantedAuthority> mapAuthorities(Set<RoleEntity> roles) {
// Map the authorities here
}
}
英文:
On Mapstruct a method can be used for the mapping on the annotations using the expression property on the annotation: expression = "java( yourJavaCodeHere )"
.
Your mapper would look like:
@Mapper(componentModel = "spring")
public abstract class AppUserDetailsDtoMapper {
@Mappings({
@Mapping(target = "username", source = "appUserLogin"),
@Mapping(target = "password", source = "appUserPassword"),
@Mapping(target = "authorities", expression = "java( mapAuthorities(user.getRoles()) )")
})
public abstract AppUserDetailsDTO toAppUserDetailsDTO(AppUserEntity user);
protected Collection<GrantedAuthority> mapAuthorities(Set<RoleEntity> roles) {
// Map the authorities here
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论