Validate JAXBElement in JPA/JAX-RS Web Service

You could handle this by creating a custom MessageBodyReader. The example below is based on a Customer model: import java.io.IOException; import java.io.InputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.net.URL; import javax.ws.rs.Consumes; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.ext.ContextResolver; import javax.ws.rs.ext.MessageBodyReader; import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Providers; import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import … Read more

How to generate CDATA block using JAXB?

Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group. If you are using MOXy as your JAXB provider then you can leverage the @XmlCDATA extension: package blog.cdata; import javax.xml.bind.annotation.XmlRootElement; import org.eclipse.persistence.oxm.annotations.XmlCDATA; @XmlRootElement(name=”c”) public class Customer { private String bio; @XmlCDATA public void setBio(String bio) { this.bio = bio; … Read more

JAXB/Moxy Unmarshalling assigns all field values to Map rather than the specific field provided for it

I believe this is somewhat related to the issue: @XmlPath(“.”) conflicts with @XmlAdapter As per bug ticket: org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl public XPathNode getNonAttributeXPathNode(String namespaceURI, String localName, String qName, Attributes attributes) { … Line 1279 if(null == resultNode && null == nonPredicateNode) { // ANY MAPPING resultNode = xPathNode.getAnyNode();// by default it return the EventAdapter returing a null … Read more

jaxb unmarshal timestamp

JAXB can handle the java.util.Date class. However it expects the format: “yyyy-MM-dd’T’HH:mm:ss” instead of “yyyy-MM-dd HH:mm:ss” If you want to use that date format I would suggest using an XmlAdapter, it would look something like the following: import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.bind.annotation.adapters.XmlAdapter; public class DateAdapter extends XmlAdapter<String, Date> { private SimpleDateFormat dateFormat = new … Read more

JAXB: How to ignore namespace during unmarshalling XML document?

Here is an extension/edit of VonCs solution just in case someone doesn´t want to go through the hassle of implementing their own filter to do this. It also shows how to output a JAXB element without the namespace present. This is all accomplished using a SAX Filter. Filter implementation: import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.XMLFilterImpl; … Read more

How to generate JAXB classes from XSD?

XJC is included in the bin directory in the JDK starting with Java SE 6. For an example see: http://blog.bdoughan.com/2010/09/processing-atom-feeds-with-jaxb.html The contents of the blog are the following: Processing Atom Feeds with JAXB Atom is an XML format for representing web feeds. A standard format allows reader applications to display feeds from different sources. In … Read more

No @XmlRootElement generated by JAXB

To tie together what others have already stated or hinted at, the rules by which JAXB XJC decides whether or not to put the @XmlRootElement annotation on a generated class are non trivial (see this article). @XmlRootElement exists because the JAXB runtime requires certain information in order to marshal/unmarshal a given object, specifically the XML … Read more

JAXB: how to marshall map into value

There may be a valid reason why you want to do this, but generating this kind of XML is generally best avoided. Why? Because it means that the XML elements of your map are dependent on the runtime contents of your map. And since XML is usually used as an external interface or interface layer … Read more