Is there a EmptyStringCheckStrategy in MapStruct?

Viewed 308

I would like to map from DTO to Entity. For that I use MapStruct. I would like to perform all String fields both on null check and emptycheck like:

if(name != null && !name.isEmpty()) {...} or much better if

(StringUtils.isNotBlank(name)) {...}

I have seen that in MapperConfig we can configure the null check with:

nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS

Is it possible to configure the empty check?

2 Answers

This is currently not possible. I remember seeing an issue in the MapStruct issue tracker about something similar. Perhaps mapstruct/mapstruct#879.

One way to do it though is to add a cust hasName() method on your object that would then use StringUtils.isNotBlank(name)

Alternatively, you can define a method:

default String toString(String in) {
   if ( in != null && !in.isEmpty() {
       return in;
   }
   return null;
}
Related