I have the object like this:
class User {
private String firstName;
private String secondName;
private int age;
....
get/set methods
}
And another object has User as a property:
class UserHolder {
private User user;
....
get/set methods
}
I want to convert UserHolder to User use MapStruct.
When I write this mapper I've something like this:
@Mapper(componentModel = "spring")
public interface UserHolderMapper {
@Mapping(source = "user.firstName", target = "firstName")
@Mapping(source = "user.secondName", target = "secondName")
@Mapping(source = "user.age", target = "age")
User toUser(UserHolder source);
}
But it contains a lot of boilerplate code (in @Mapping annotation), how I can say to mapper that I want to map source.user to this target without specifying fields of target?
I may be late to the party. However following should work fine.
@Mapper(componentModel = "spring")
public interface UserHolderMapper {
@Mapping(source = "source.user", target = ".")
User toUser(UserHolder source);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With