I have this requirement in which for all the String -> String mappings, I need to check if the source string is empty and if it is I want to return null. I thought this would work but unfortunately doesn't
@Mapping(source = "in", target = ".", qualifiedByName = "stringConverter")
abstract String mapString(String in);
I thought this would generate a function like
String mapString(String in) {
if ( in == null ) {
return null;
}
String string = stringConverter(in);
return string;
}
Or something similar. Here stringConverter just basically checks if the string is empty and returns null if so, else the original string. But in turn what I actually get is
String mapString(String in) {
if ( in == null ) {
return null;
}
String string = new String();
return string;
}
Is it possible to create a generic function that handles mappings for all the string fields when using mapstruct?
MapStruct is meant for mapping beans and not for mapping between Strings.
You need to write a custom method for mapping between String
and MapStruct will use that method.
public class StringConverter {
public static String map(String value) {
return (value == null || value.isEmpty()) ? null : value;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.