Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a global setting in Mapstruct that will trim a string value prior to setting it to a destination bean property

Tags:

mapstruct

Is it possible to trim a string value before it is set against a bean property of type string in the destination bean?

Dozer offers such a facility through its mapping configuration for example,

<configuration>
    <trim-strings>true</trim-strings>
</configuration>

Also see Dozer Global Configuration

With MapStruct 1.0.0.Final I can achieve this through Expressions or Before/After Mapping customization.

But wanted to know if there is a better way to handle such use cases.

Thanks in advance.

like image 370
Venkat Srinivasan Avatar asked Aug 01 '16 16:08

Venkat Srinivasan


People also ask

What is MappingTarget in MapStruct?

Annotation Type MappingTargetDeclares a parameter of a mapping method to be the target of the mapping. Not more than one parameter can be declared as MappingTarget . NOTE: The parameter passed as a mapping target must not be null .

How do I set default value in MapStruct?

Java Prime Pack Using Mapstruct we can pass the default value in case source property is null using defaultValue attribute of @Mapping annotation.

What is the use of @mapper annotation?

The @Mapper annotation 1 marks the interface as mapping interface and lets the MapStruct processor kick in during compilation. The actual mapping method 2 expects the source object as parameter and returns the target object. Its name can be freely chosen.


1 Answers

It appears MapStruct in its current form does not support this.

However one can achieve this effect with custom mapper methods, for example implement a class with a method that trims a String argument passed to it and then reference this class in the use attribute of the @Mapper annotation. More at Invoking other mappers

If you require fine gained access control you could use Selection based on Qualifiers

I was made aware of these approaches in response to a question I posted in mapstruct Google group

like image 50
Venkat Srinivasan Avatar answered Nov 16 '22 10:11

Venkat Srinivasan