【发布时间】:2021-10-05 00:43:48
【问题描述】:
我想与大家讨论解决这个问题的最佳方法。 我有一个像这样的java对象,它是一个源代码
public class Source{
private String customValue;
private List<String> listOfCustomValues;
//etc
}
当然,我有一个目标 java 对象
public class Target{
private String ownCustomValue;
//etc
}
现在,我会以这种方式(或类似的方式)进行映射
@Mapper
public abstract class MapperSourceToTarget {
@BeanMapping(ignoreByDefault = true)
@Mapping(target = "ownCustomValue", qualifiedByName = "customMapping")
public abstract Target fromSourceToTarget ( Source source);
@Named("customMapping")
public String toFuelType(Source source) {
String firstValue = source.getListOfCustomValues().get(0);
return ( firstValue.isEmpty() || firstValue == null ) ? source.getCustomValue() : firstValue;
}
}
您对这种行为有什么建议或批评吗?我可以用更好的方式做到这一点吗?
【问题讨论】: