Reputation: 1084
I have simple object Client
public class Client {
String externalCode;
String name;
String surname;
}
And I want to map it to nearly identical object
public class User {
String internalCode;
String name;
String surname;
}
See, I want externalCode to be mapped to internalCode. And I have a method which does that. I have marker my method with my custom @CodeMapping annotation and put that annotation to qualifiedBy parameter. So, here is my mapper.
@Mapper()
ClientMapper {
@CodeMapping
String toInternalCode(String externalCode) {
return externalCode + " internal part";
}
@Mapping(target = "internalCode", source = "externalCode", qualifiedBy = CodeMapping.class)
User toUser(Client client);
}
The problem is that name and surname fields are also mapped using toInternalCode method. Mapstruct sees that I have defined a method, which maps String to String and thinks it should be used in all cases.
Is there a way to tell Mapstruct, that if no qualifiers are specified, direct mapping should be used? Or make my own method which takes string and return it and tell Mapstruct it should use that method by default?
Upvotes: 0
Views: 2910
Reputation: 21403
Most likely the toInternalCode
is used by all methods because the @CodeMapping
annotation is not meta annotated with @Qualifier
(from org.mapstruct.Qualifier
).
The @CodeMapping
should be defined in the following way:
import org.mapstruct.Qualifier;
@Qualifier
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface CodeMapping {
}
Upvotes: 1