Java 如何使用编程 api 告诉 Dozer 绕过每个字段的空或空字符串值的映射?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19256952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I tell Dozer to bypass mapping null or empty string values per field using the programming api?
提问by Daniel Kaplan
The faq explainshow to do this in XML. This shows how to do it per class using the API. The problem is you have to set it on on the class. What if I only want it on one field? Is this possible?
常见问题解答解释了如何在 XML 中执行此操作。这显示了如何使用 API为每个类执行此操作。问题是你必须在课堂上设置它。如果我只想要一个字段怎么办?这可能吗?
采纳答案by Daniel Kaplan
I was able to accomplish what I needed by creating my own custom converters for this. Here's the code:
通过为此创建我自己的自定义转换器,我能够完成我所需要的。这是代码:
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);
}
}
Second one:
第二个:
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;
}
}
}
回答by aviad
For the sake of future readers: the solution with the custom converter did not work for me. It seems that the converters are just being ignored during the mapping.
为了将来的读者:使用自定义转换器的解决方案对我不起作用。似乎转换器在映射过程中被忽略了。
However, defining custom field mapper did work quite well: (written in java 8)
但是,定义自定义字段映射器确实工作得很好:(用 java 8 编写)
dozerBeanMapper.setCustomFieldMapper((source, destination, sourceFieldValue, classMap, fieldMapping) ->
sourceFieldValue == null);
This mapper returns a boolean indicating weather the mapping was done for that field. So, returning true if the object is null, will notify Dozer that the mapping was finished. Sending false will continue the default mapping. (See MappingProcessor.java - mapField method)
此映射器返回一个布尔值,指示为该字段完成映射的天气。因此,如果对象为 null,则返回 true,将通知 Dozer 映射已完成。发送 false 将继续默认映射。(参见 MappingProcessor.java - mapField 方法)
回答by cheb1k4
You don't need converter. Dozer allows to do it easily:
你不需要转换器。推土机允许轻松做到这一点:
import static org.dozer.loader.api.TypeMappingOptions.mapNull;
import static org.dozer.loader.api.TypeMappingOptions.mapEmptyString;
...
public Mapper getMapper() {
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.addMapping(beanMappingBuilder());
return mapper;
}
private BeanMappingBuilder beanMappingBuilder() {
return new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(ClassA.class, ClassB.class, mapNull(false), mapEmptyString(false));
}
}
}
回答by Dotcomwizard
you can use the following to bypass null value in xml configuration
您可以使用以下内容绕过xml配置中的空值
<mapping map-null="false">
<class-a>org.dozer.vo.AnotherTestObject</class-a>
<class-b>org.dozer.vo.AnotherTestObjectPrime</class-b>
<field>
<a>field4</a>
<b>to.one</b>
</field>
</mapping>