作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经被这个麻烦困扰了两天了,但我无法解决。我有:
public class ClientBo{
...
List<PersonBo> person;
...
}
和
public class ClientVo{
...
PersonVo person;
...
}
我需要做的是以某种方式配置推土机,这样我就可以从 PersonBo 列表映射到单个字段 PersonVo(Vo 和 Bo 具有相同的字段名称)。
Dozer 有一个内置函数,可以从集合转换为单个字段,但不能以其他方式转换。 http://dozer.sourceforge.net/documentation/faq.html#mult-fields-to-single-field
我想到的唯一解决方案是:
<mapping type="one-way">
<class-a>...ClientBo</class-a>
<class-b>...ClientVo</class-b>
<field>
<a>person[0]</a>
<b>person</b>
</field>
</mapping>
<mapping type="one-way">
<class-a>...ClientVo</class-a>
<class-b>...ClientBo</class-b>
<field custom-converter="mapper.CustomObjectToList">
<a>person</a>
<b>person</b>
</field>
</mapping>
和
public class CustomObjectToList implements CustomConverter{
public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
if(sourceFieldValue==null)
return null;
if(sourceFieldValue instanceof List && ((List<?>) sourceFieldValue).size()>0){
/* This if is an attempt to get the first element of the
list and return it as a single field, but id doesn't work*/
Object o = ((List<?>)sourceFieldValue).get(0);
return o;
}else{
/*Here a single field is put in a List and returned*/
ArrayList<Object> result = new ArrayList<>();
result.add(sourceFieldValue);
return result;
}
}
}
有什么办法可以删除
<mapping type="one-way">
<class-a>...ClientBo</class-a>
<class-b>...ClientVo</class-b>
<field>
<a>person[0]</a>
<b>person</b>
</field>
</mapping>
并通过自定义转换器完成工作?它应该尽可能更通用,以便它可以适应类似的上下文。
谢谢!
最佳答案
<强>1。您是否尝试测试您的 CustomConverter?
因为如果您尝试打印任何变量,您将得到一个 ClassCastException
,如下所示:
Exception in thread "main" java.lang.ClassCastException: beans.PersonVo cannot be cast to beans.PersonBo
at execute.Execute.main(Execute.java:37)
是的,具有相同名称的字段会自动映射,但在您的情况下,您使用 CustomConverter 来进行映射。换句话说,Dozer 信任您来处理它,因此您不能简单地将一个类变量 (personVo) 指向另一个不相关的类实例 (personBo)。
<强>2。纠正上述异常
纠正上述异常的最简单方法是使用 setter 和 getter 方法,就像处理任何不相关的类一样:
public class CustomObjectToList implements CustomConverter{
public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
if(sourceFieldValue==null)
return null;
if(sourceFieldValue instanceof List && ((List<?>) sourceFieldValue).size()>0){
/* This if is an attempt to get the first element of the
list and return it as a single field, but id doesn't work*/
Object o = ((List<?>)sourceFieldValue).get(0);
return o;
}else{
/*Updated: using a setter and getter*/
PersonVo source = (PersonVo) sourceFieldValue;
List<PersonBo> dest = new ArrayList<PersonBo>();
PersonBo bo = new PersonBo();
bo.setName(source.getName());
dest.add(bo);
return dest;
}
}
}
<强>3。回答你的问题
要取消单向映射并仅使用 CustomConverter,您可以使用以下内容:
推土机 XML:
<!-- <mapping type="one-way">
<class-a>beans.ClientBo</class-a>
<class-b>beans.ClientVo</class-b>
<field>
<a>person[0]</a>
<b>person</b>
</field>
</mapping> -->
<mapping>
<class-a>beans.ClientVo</class-a>
<class-b>beans.ClientBo</class-b>
<field custom-converter="converter.CustomObjectToList">
<a>person</a>
<b>person</b>
</field>
</mapping>
自定义转换器:
public class CustomObjectToList implements CustomConverter{
public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
if(sourceFieldValue==null)
return null;
if(sourceFieldValue instanceof PersonVo){
PersonVo source = (PersonVo) sourceFieldValue;
List<PersonBo> dest = new ArrayList<PersonBo>();
PersonBo bo = new PersonBo();
bo.setName(source.getName());
dest.add(bo);
return dest;
}else if (sourceFieldValue instanceof List<?>){
List<PersonBo> source = (List<PersonBo>) sourceFieldValue;
PersonVo dest = new PersonVo();
dest.setName(source.get(0).getName());
return dest;
}
else {
throw new MappingException("Converter TestCustomConverter "
+ "used incorrectly. Arguments passed in were:"
+ existingDestinationFieldValue + " and " + sourceFieldValue);
}
}
}
关于java - 如何将集合中包含的对象映射到字段,反之亦然?推土机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42233578/
我是一名优秀的程序员,十分优秀!