Reputation: 15917
Dozer is not calling my CustomConverter
no matter what I seem to do. I tried to put breakpoints and none of the CustomerConverter
code is ever called. The objects are converting, all fields but the ones linked to the CustomConverter
are working.
In my dozerBeanMapping.xml
file I have:
<configuration>
<custom-converters>
<converter type="converter.JodaDateToJavaDateConverter">
<class-a>org.joda.time.LocalDate</class-a>
<class-b>java.util.Date</class-b>
</converter>
</custom-converters>
</configuration>
<mapping type="one-way">
<class-a>data.SourceObject</class-a>
<class-b>data.DestinationObject</class-b>
<field custom-converter="converter.JodaDateToJavaDateConverter">
<a>myLocalDate</a>
<b>myJavaDate</b>
</field>
</mapping>
Then for the converter I have:
package converter;
public class JodaDateToJavaDateConverter implements CustomConverter
{
@Override
public Object convert(Object destination, Object source, Class<?> destinationClass, Class<?> sourceClass)
{
if(source == null)
return null;
if(!(source instanceof LocalDate))
throw new MappingException("Misconfigured/unsupported mapping");
return ((LocalDate)source).toDateTimeAtStartOfDay().toDate();
}
}
And for the Objects I have:
package data;
public class SourceObject
{
private LocalDate myLocalDate = LocalDate.now();
public void setMyLocalDate(LocalDate myLocalDate) { this.myLocalDate = myLocalDate; }
public LocalDate getMyLocalDate() { return myLocalDate; }
}
package data;
public class DestinationObject
{
private Date myJavaDate;
public void setMyJavaDate(Date myJavaDate) { this.myJavaDate = myJavaDate; }
public Date getMyJavaDate() { return myJavaDate; }
}
I don't think makes any difference but here is the code to start it all:
SourceObject mySourceObject = DozerBeanMapperSingletonWrapper.getInstance().map(DestinationObject, SourceObject.class);
I have no idea why my custom converters are not called...
UPDATE: I'm even more confused now. If the fields are the same name for both the Destination and Source Object then everything works. For whatever reason I can't map fields with different names...
Upvotes: 3
Views: 1601
Reputation: 1299
In your mapping call, you are mapping DestinationObject to SourceObject, while in the dozerBeanMapping.xml
, you specify that your custom converter should only be used when mapping SourceObject to DestinationObject, and not the other way round (due to type=one-way
).
When you use same field names, the general config becomes active, where you specified that every org.joda.time.LocalDate
should be mapped to a java.util.Date
(and vice-versa) using your custom converter.
Upvotes: 2