tryingHard
tryingHard

Reputation: 2084

Mapstruct optional mapping

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>

Upvotes: 0

Views: 2656

Answers (1)

M. Deinum
M. Deinum

Reputation: 124536

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.

Upvotes: 3

Related Questions