Is there a way to map an empty String to null using MapStruct?
Asked Answered
D

4

13

I am using MapStruct for mapping a DTO to an entity and vice versa in my Spring Boot application.

Is there a way to map empty strings to null using @Mapping?

Drakensberg answered 16/4, 2020 at 11:42 Comment(0)
S
20

You can simply use the expression within the @Mapping

@Mapping(target = "name", expression = "java(source.name.isEmpty() ? null : source.name)")
Dog convert(Cat source);
Sherr answered 18/4, 2020 at 9:12 Comment(2)
If the field has a private access, we may use the getter method in place of the field name. Eg: @Mapping(target = "name", expression = "java(source.getName.isEmpty() ? null : source.getName)") Dog convert(Cat source);Vestavestal
What a boilerplate!Presidium
L
20

Using an expression, as suggested in another answer, is possible. I personally prefer to do this a bit safer, and perhaps a bit more declarative.

@Mapper
public interface MyMapper {

    @Mapping( target = "s2",  qualifiedBy = EmptyStringToNull.class )
    Target map( Source source);

    @EmptyStringToNull
    default String emptyStringToNull(String s) {
        return s.isEmpty() ? null : s;
    }

    @Qualifier
    @java.lang.annotation.Target(ElementType.METHOD)
    @Retention(RetentionPolicy.CLASS)
    public @interface EmptyStringToNull {
    }

    class Source {

        private String s1;
        private String s2;

        public String getS1() {
            return s1;
        }

        public void setS1(String s1) {
            this.s1 = s1;
        }

        public String getS2() {
            return s2;
        }

        public void setS2(String s2) {
            this.s2 = s2;
        }
    }

    class Target {

        private String s1;
        private String s2;

        public String getS1() {
            return s1;
        }

        public void setS1(String s1) {
            this.s1 = s1;
        }

        public String getS2() {
            return s2;
        }

        public void setS2(String s2) {
            this.s2 = s2;
        }
    }
}


You can re-use the EmptyStringToNull qualifier as many times as you'd like and your not dependent on the parameter name.

Lanti answered 25/4, 2020 at 7:55 Comment(1)
Is there any way to do that for all strings in all mappers?Editorial
J
3

If you would like it to work globally in the mapper class and intercept any String-Mappings you can simply implement the following:

default String mapEmptyString(String string) {
 string != null && !string.isEmpty() ? string : null;
}

This will map any null or empty String source to null.

Jesusitajet answered 24/1, 2022 at 12:37 Comment(1)
Are you sure this works?Presidium
B
0

You can also use a Condition like the documentation suggests.

@Mapper
public interface MyMapper {

     Target map( Source source);

     @Condition
     default boolean isNotEmpty(String value) {
        return value != null && !value.isEmpty();
     }
}
Bestraddle answered 14/3, 2024 at 18:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.