Is there a way to change the default way jaxb serialize/deserialize types, dates in my case, without specifying it through annotation and/or through xml jaxb binding as mentioned here http://jaxb.java.net/guide/Using_different_datatypes.html
I'd basically like to do something like:
JAXBContext jaxbContext = ...;
Marshaller marshaller = jaxbContext.createMarshaller().setAdapter(new DateAdapter(dateFormat));
To have a preconfigured JaxBContext or Marshaller/Unmarshaller that serialize/deserialize dates in a customized way..
Couldn't find any resource that shows how to do expect through annotations or statically with the xml binding file.. Thanks!
In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.
JAXB stands for Java Architecture for XML Binding. It provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object. Simply, you can say it is used to convert java object into xml and vice-versa.
All other objects, including Marshaller and Unmarshaller, are not thread-safe and must not be shared. The static helper methods in the JAXB class can be used from several threads, of course. In practice, this means that if you need a JAXBContext instance, you should probably store in a static member.
The Marshaller class is responsible for governing the process of serializing Java content trees back into XML data.
This isn't exactly what you're looking for but it beats annotating every Date field individually. You can set a XmlJavaTypeAdapter at the package level so that every reference to Date within your package will use it. If your objects are in the com.example package, you should add a package-info.java file to it with the following contents:
@XmlJavaTypeAdapter(value=MyCustomDateAdapter.class,type=Date.class)
package com.example;
Try this:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement(name = "event")
public class Event {
private Date date;
private String description;
@XmlJavaTypeAdapter(DateFormatterAdapter.class)
public Date getDate() {
return date;
}
public void setDate(final Date date) {
this.date = date;
}
public String getDescription() {
return description;
}
public void setDescription(final String description) {
this.description = description;
}
private static class DateFormatterAdapter extends XmlAdapter<String, Date> {
private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd_mm_yyyy");
@Override
public Date unmarshal(final String v) throws Exception {
return dateFormat.parse(v);
}
@Override
public String marshal(final Date v) throws Exception {
return dateFormat.format(v);
}
}
public static void main(final String[] args) throws Exception {
final JAXBContext context = JAXBContext.newInstance(Event.class);
final Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
final Event event = new Event();
event.setDate(new Date());
event.setDescription("im rick james");
marshaller.marshal(event, System.out);
}
}
This produces:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<event>
<date>16_05_2011</date>
<description>im rick james</description>
</event>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With