admin管理员组文章数量:1183762
I have a DTO that doesn't work with mapping
@Data
public class IndividualDTO {
private String passportNumber;
private String phoneNumber;
private UserDTO user;
@Data
public static class UserDTO {
@NotBlank
@Email
private String email;
private String secretKey;
private String firstName;
private String lastName;
private AddressDTO address;
}
@Data
public static class AddressDTO {
private String address;
private String city;
private String state;
private String zipCode;
private CountryDTO country;
}
@Data
public static class CountryDTO{
private String name;
}
}
I am trying to map using MapStruct in this way
IndividualMapper
@Mapper(componentModel = "spring", uses = {UserMapper.class, AddressMapper.class, CountryMapper.class})
public interface IndividualMapper {
IndividualMapper INSTANCE = Mappers.getMapper(IndividualMapper.class);
Individual toEntity(IndividualDTO dto);
IndividualDTO toDto(Individual entity);
}
AddressMapper
@Mapper(componentModel = "spring")
public interface AddressMapper {
Address toAddressEntity(IndividualDTO.AddressDTO addressDTO);
IndividualDTO.AddressDTO toAddressDto(Address address);
}
CountryMapper
@Mapper(componentModel = "spring")
public interface CountryMapper {
Country toCountryEntity(IndividualDTO.CountryDTO countryDTO);
IndividualDTO.CountryDTO toCountryDto(Country country);
}
UserMapper
@Mapper(componentModel = "spring")
public interface UserMapper {
User toUserEntity(IndividualDTO.UserDTO userDTO);
IndividualDTO.UserDTO toUserDto(User user);
}
As a result, I get:
@Component
public class IndividualMapperImpl implements IndividualMapper {
@Override
public Individual toEntity(IndividualDTO dto) {
if ( dto == null ) {
return null;
}
Individual individual = new Individual();
return individual;
}
@Override
public IndividualDTO toDto(Individual entity) {
if ( entity == null ) {
return null;
}
IndividualDTO individualDTO = new IndividualDTO();
return individualDTO;
}
}
According to the MapStruct documentation, everything should work, but unfortunately I ran into this problem.
MapStruct generated the IndividualMapperImpl class for me, which has no logic
I have a DTO that doesn't work with mapping
@Data
public class IndividualDTO {
private String passportNumber;
private String phoneNumber;
private UserDTO user;
@Data
public static class UserDTO {
@NotBlank
@Email
private String email;
private String secretKey;
private String firstName;
private String lastName;
private AddressDTO address;
}
@Data
public static class AddressDTO {
private String address;
private String city;
private String state;
private String zipCode;
private CountryDTO country;
}
@Data
public static class CountryDTO{
private String name;
}
}
I am trying to map using MapStruct in this way
IndividualMapper
@Mapper(componentModel = "spring", uses = {UserMapper.class, AddressMapper.class, CountryMapper.class})
public interface IndividualMapper {
IndividualMapper INSTANCE = Mappers.getMapper(IndividualMapper.class);
Individual toEntity(IndividualDTO dto);
IndividualDTO toDto(Individual entity);
}
AddressMapper
@Mapper(componentModel = "spring")
public interface AddressMapper {
Address toAddressEntity(IndividualDTO.AddressDTO addressDTO);
IndividualDTO.AddressDTO toAddressDto(Address address);
}
CountryMapper
@Mapper(componentModel = "spring")
public interface CountryMapper {
Country toCountryEntity(IndividualDTO.CountryDTO countryDTO);
IndividualDTO.CountryDTO toCountryDto(Country country);
}
UserMapper
@Mapper(componentModel = "spring")
public interface UserMapper {
User toUserEntity(IndividualDTO.UserDTO userDTO);
IndividualDTO.UserDTO toUserDto(User user);
}
As a result, I get:
@Component
public class IndividualMapperImpl implements IndividualMapper {
@Override
public Individual toEntity(IndividualDTO dto) {
if ( dto == null ) {
return null;
}
Individual individual = new Individual();
return individual;
}
@Override
public IndividualDTO toDto(Individual entity) {
if ( entity == null ) {
return null;
}
IndividualDTO individualDTO = new IndividualDTO();
return individualDTO;
}
}
According to the MapStruct documentation, everything should work, but unfortunately I ran into this problem.
MapStruct generated the IndividualMapperImpl class for me, which has no logic
Share Improve this question asked Jan 26 at 11:27 448844448844 253 bronze badges 1- This question is similar to: MapStruct + Lombok together not compiling: unknown property in result type. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Luca Basso Ricci Commented Jan 27 at 7:12
1 Answer
Reset to default 0According to the mapstruct FAQ, to use Lombok (1.18.16 version or newer) and Mapstruct together, you have to add lombok-binding
path to the config of the maven-compiler plugin:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<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>${org.projectlombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
So please check pom file to make sure it is configured correctly according to the Mapstruct doc.
Additional comments:
- You don't need to declare an INSTANCE variable in the mapper (like you did in
IndividualMapper
), since you're usingcomponentModel = "spring"
. You can simple autowire your mapper into other spring beans:
@Service
public class SomeService {
@Autowired
private IndividualMapper individualMapper;
}
- You declared the
uses
attribute of the@Mapper
annotation only forIndividualMapper
with all mappers (even though it only needs to use theUserMapper
), and did not tell the other mappers which mappers they should use. This results inUserMapper
not usingAddressMapper
, as intended (Check this in the generated mapperImpl-s). So add theuses
attribute with the appropriate values to the relevant mappers:
@Mapper(componentModel = "spring", uses = UserMapper.class)
public interface IndividualMapper {
Individual toEntity(IndividualDTO dto);
IndividualDTO toDto(Individual entity);
}
@Mapper(componentModel = "spring", uses = AddressMapper.class)
public interface UserMapper {
Individual.User toUserEntity(IndividualDTO.UserDTO userDTO);
IndividualDTO.UserDTO toUserDto(Individual.User user);
}
// and so on for other mappers
本文标签: javaMapStruct mapper not mapping nested DTOs properly in spring bootStack Overflow
版权声明:本文标题:java - MapStruct mapper not mapping nested DTOs properly in spring boot - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738318988a2074412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论