HungryForKnowledge
HungryForKnowledge

Reputation: 289

Mapping XMLGregorianCalendar via Dozer

I am trying to map two attributes, both XMLGregorianCalendar type. Trying to achieve this via dozer, values are getting mapped but am getting wierd date time values like below

Input in XML

 <urn1:ReservationDate>2015-02-11</urn1:ReservationDate>
 <urn1:ReservationTime>03:28:00</urn1:ReservationTime>

Output in JSON

 "reservationDate": 1423593000000,
  "reservationTime": -7320000

My dozer mappings are as below

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">
    <configuration>
        <stop-on-errors>true</stop-on-errors>
        <date-format>MM/dd/yyyy HH:mm</date-format>
        <!-- <date-format>MM-dd-yyyy</date-format> -->
        <wildcard>true</wildcard>
        <custom-converters>
        <converter type="com.xxx.util.XMLGC2XMLGC">
        <class-a>javax.xml.datatype.XMLGregorianCalendar</class-a>
        <class-b>javax.xml.datatype.XMLGregorianCalendar</class-b>
        </converter>
        </custom-converters>
    </configuration>

    <mapping>
        <class-a>com.xxx..ReservationType
        </class-a>
        <class-b>com.xxx..ReservationDto
        </class-b>
        <field>
            <a>pnrLocator</a>
            <b>pnrLocator</b>
        </field>
        <field>
            <a>reservationDate</a>
            <b>reservationDate</b>
            <a-hint>java.util.GregorianCalendar</a-hint>
        </field>
    </mapping>
</mappings> 

XMLGC2XMLGC class code

public class XMLGC2XMLGC extends
DozerConverter<XMLGregorianCalendar, XMLGregorianCalendar> {
    public XMLGC2XMLGC() {
        super(XMLGregorianCalendar.class, XMLGregorianCalendar.class);
    }
    @Override
    public XMLGregorianCalendar convertFrom(XMLGregorianCalendar src,
            XMLGregorianCalendar dest) {
        return src;
    }
    @Override
    public XMLGregorianCalendar convertTo(XMLGregorianCalendar src,
            XMLGregorianCalendar dest) {
        return dest;
    } 

I even tried Date to XMLGregorianCalendar conversion as I read some documentation that said it should happen automatically but I am getting exception like this when doing the same

java.lang.IllegalAccessException: Class org.dozer.util.ReflectionUtils can not access a member of class org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl with modifiers \"public\"

Please help

Upvotes: 1

Views: 3004

Answers (1)

HungryForKnowledge
HungryForKnowledge

Reputation: 289

The issue was with JSON serialization here. I had to write a Custom Serializer for displaying XMLGregorianCalendar date correctly in JSON

Code is as below

public class CustomDateSerializer extends JsonSerializer<XMLGregorianCalendar> {    
    @Override
    public void serialize(XMLGregorianCalendar value, JsonGenerator gen, SerializerProvider arg2) throws 
        IOException, JsonProcessingException {      

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = formatter.format(value.toGregorianCalendar().getTime());
        gen.writeString(formattedDate);

    }
}

And annotated the respective field as below

@JsonSerialize(using = CustomDateSerializer.class)
    private XMLGregorianCalendar reservationDate;

This was enough to get correct date format in JSON :)

Hope this helps someone !

Upvotes: 2

Related Questions