简体   繁体   中英

Mapstruct: How to default a target String to Empty String when the Source is Null (Both fields have the same name and type) Java / Spring

I have two Objects Source and Target both with the same field names and types.

If a source field is null I would like the target to be "" (Empty String)

My Interface mapping looks like this (This is just two field, I have many)

@Mapper(componentModel = "spring", nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface MyMapper {

@Mappings({
    @Mapping(target="medium", defaultExpression="java(\"\")"),
    @Mapping(target="origin", defaultExpression="java(\"\")")
 }) 
public Target mapFrom(Source source)

If the Source has a value it should be copied across, if it is null in the source it should be "" in the target.

Mapstruct-1.3.0 seems to just keep everything null.

Any Idea? I would like default to be empty String for everything

You need to set the NullValuePropertyMappingStrategy (as part of the Mapper annotation) for defining how null properties are to be mapped.

See NullValuePropertyMappingStrategy.html#SET_TO_DEFAULT

The default value for String is "" . You don't need to define it explicitly.

So, your mapper can simply look like this:

@Mapper(
    componentModel = "spring", 
    nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT, 
    nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT
)
public interface MyMapper {

    public Target mapFrom(Source source);

}

When your Source object has the same fields as Target object and when you want to manage all Source null values (eg for String) to became an empty String ("") in the Target object, you could create mapper interface from MapStruct library as below:

Step 1:

@Mapper(componentModel = "spring")
public interface SourceToTargetMapper {

  Target map(Source source);

  @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT)
  void update(Source source, @MappingTarget Target target);
}

The whole trick is to define nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT but you cannot define it in @Mapper annotation. Instead of that you had to place it as parameter in @BeanMapping annotation for update() method. You can read more about this in MapStruct documentation .

Step 2:

Therefore you had to do one more operation in your code and use just implemented 'update()' method:

@Component
public class ClassThatUsingMapper {

  private final SourceToTargetMapper mapper;

  public Target someMethodToMapObjects(Source source) {
    Target target = mapper.map(source);
    mapper.update(source, target)

    return target;
  }
}

All null to empty String process takes place under mapper.update(source, target) method. After run mvn clean install for your project, you can check how it looks and how it works in target/generated-sources/annotations/...../SourceToTargetMapperImpl.java file.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM