尝试映射日期 -> 日历时遇到 InstantiationException。
简单测试如下:
@Test
public void testConversion()
{
GregorianCalendar cal = new GregorianCalendar(2009, 2, 3);
Date sourceValue = cal.getTime();
DozerBeanMapper mapper = new DozerBeanMapper();
Object result = mapper.map(sourceValue, Calendar.class);
}
根据docs ,这是开箱即用的支持(即使日历是抽象的)。有人有这方面的经验并能够指出我做错了什么吗?
最佳答案
你是对的。这会抛出InstantionException
(我认为这是推土机中的一个错误。您会将其记录在他们的错误跟踪系统中吗?)。
但是。当您不在根级别转换日期 <--> 日历值时,它会起作用。这个测试对我有用(推土机 5.1):
public static class Source {
private Date value;
public void setValue(Date value) {
this.value = value;
}
public Date getValue() {
return value;
}
}
public static class Target {
private Calendar value;
public void setValue(Calendar value) {
this.value = value;
}
public Calendar getValue() {
return value;
}
}
@Test
public void testConversion()
{
final GregorianCalendar cal = new GregorianCalendar(2009, 2, 3);
Source source = new Source(){{ setValue(cal.getTime());}};
DozerBeanMapper mapper = new DozerBeanMapper();
Target result = (Target) mapper.map(source, Target.class);
assertEquals(cal.getTimeInMillis(), result.getValue().getTimeInMillis());
}
关于java - Dozer InstantiationException 映射 Calendar 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1391210/