问题描述
为此,我可以创建自己的自定义转换器来实现所需的功能。这是代码:
import org.apache.commons.lang.StringUtils;
import org.dozer.DozerConverter;
public class IfNotBlankConverter extends DozerConverter<String, String> {
public IfNotBlankConverter() {
super(String.class, String.class);
}
private String getobject(String source, String destination) {
if (StringUtils.isNotBlank(source)) {
return source;
} else {
return destination;
}
}
@Override
public String convertTo(String source, String destination) {
return getobject(source, destination);
}
@Override
public String convertFrom(String source, String destination) {
return getobject(source, destination);
}
}
第二个:
import org.dozer.DozerConverter;
public class IfNotNullConverter extends DozerConverter<Object, Object> {
public IfNotNullConverter() {
super(Object.class, Object.class);
}
@Override
public Object convertTo(Object source, Object destination) {
return getobject(source, destination);
}
@Override
public Object convertFrom(Object source, Object destination) {
return getobject(source, destination);
}
private Object getobject(Object source, Object destination) {
if (source != null) {
return source;
} else {
return destination;
}
}
}