我想知道如何使用mapping
更改此mapstruct
以避免nullPointer异常。rep.getClientLevelType()
可以为null,也可以为DIRECT
或
RELATED
。在fromValue
或null
上执行empty.string
时,会发生空指针异常。
我不知道该怎么做,仅当使用rep.getClientLevelType()
的mapstruct
不为null时才显示此字段。
@Mapping(target = "clientLevelType", expression = "java(ClientLevelType.fromValue(rep.getClientLevelType()))")
从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>
最佳答案
可以通过mapstruct隐式地将String
转换为enum
,请参见文档中的implicit type conversions。
因此,不用添加expression
而是直接添加source
,或者当字段名称匹配时,您甚至可以将其忽略,然后MapStruct将自动检测映射。
关于java - Mapstruct可选映射,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52404850/