Mapstruct optional mapping

tryingHard

I am wondering how can i change this mapping using mapstruct to avoid nullPointer exception. rep.getClientLevelType() can be null, or can be DIRECT or RELATED. When fromValue executes on null or empty.string null pointer exception occurs. I don't know how to do that this field is only present if rep.getClientLevelType() is not null using mapstruct.

@Mapping(target = "clientLevelType", expression = "java(ClientLevelType.fromValue(rep.getClientLevelType()))")

enum document generated from wsdl:

@XmlType(name = "ClientLevelType")
@XmlEnum
public enum ClientLevelType {

    DIRECT,
    RELATED;

    public String value() {
        return name();
    }

    public static ClientLevelType fromValue(String v) {
        return valueOf(v);
    }    
}

WSDL:

   <xs:simpleType name="ClientLevelType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="DIRECT"/>
            <xs:enumeration value="RELATED"/>
        </xs:restriction>
    </xs:simpleType>
M. Deinum

Converting a String to an enum can be done implicitly by mapstruct, see implicit type conversions in the documentation.

So instead of adding an expression simply add the source or when the names of the fields match you could even leave those out and MapStruct will then automatically detect the mapping.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related