JAXB : Is the annotation #XmlAccessorType is only for Serialization and nothing to do with Binding of data? - jaxb

I wanted to know why do we need to specify the Annotation #XmlAccessorType when working with JAXB .
When i googled for this i found out this description from a website stating this
#XmlAccessorType sets default field and property serializability. By default, JAXB serializes public fields and properties. By setting #XmlAccessorType, the bean can choose to only allow annotated fields to be serialized.
Here the author mentions that with this annotation it gives control on serialization .
My question is , so #XmlAccessorType has nothing to do with the JAXB Binding and Unbinding from XML to java and java to XML , and it is all about Serialization only .

JAXB's #XmlAccessorType annotation is only used by JAXB (JSR-222) implementations for determining how to marshal a file to/from XML:
Normally the main decision to be made is between FIELD & PROPERTY/PUBLIC. FIELD is particularly useful when you have logic in your get/set methods that you do not want triggered during marshalling/unmarshalling. To see one way this choice affects the mapping metadata see:
http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html
NONE is a useful choice when you have many unmapped properties and you want to tell your JAXB implementation to only map the fields/properties you have annotated. This can be alot easier than adding a lot of #XmlTransient annotations into your model.
Fore More Information
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

Related

HibernateMapping & JAXB

Can I use same Pojo class for hibernateMapping and jaxb binding ?
For example, I have a username in my Pojo class and hibernate mapping bind username with a database field.
I would like to bind this username with xml properties and send that with soap.
Is it possible ?
Thanks for your help.
Yes. In fact hyperjaxb3 is an open source library that facilitates this process (there are likely others available).
If you want to do it by hand, all you need to do is put both JAXB and JPA (or whatever) annotations on your POJO.

wsgen fails to ignore JAXB annotations

I've been using Metro and have come across a problem whereby wsgen seems to ignore any JAXB annotation that I put on web method response classes.
I'm trying to return a set of entity classes that I want to expose to external clients via a web service, however I only want to make certain class attributes and methods visible to external clients, so have set the class access type to be PROPERTY, and have annotated all attributes I want to be part of the XML version of the class with #XmlElement.
I can create the XML schema for the entity class using the schemagen ant task, which correctly ignores any attribute or method that I have not annotated with #XmlElement, however when I run wsgen to create the wsdl for the web service which has a method which returns a set of the entity class, then I get a java.lang.NoClassDefFoundError. This appears to be because wsgen is not ignoring a public method on the entity class that it should do, with the class referenced by the exception being a parameter to this method.
I've tried to annotate the method with #XmlTransient so that wsgen ignores it, but to no avail. If anyone can suggest what I should do to get wsgen to pay attention to my annotations then that would be great.
An annotated field/property will always be processed regardless of accessor type. You need to specify #XmlAccessorType(XmlAccessType.NONE) if you only want annotated fields/properties to be treated as mapped.
For More Information
http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html

How to force JAXB marshaller to use xlink references?

I use JAXB marshaller to store some java objects as XML files. Some of these objects reference each other, so I unsurprisingly obtain this error:
[com.sun.istack.internal.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML
The solution which consists in removing the cycles and use only tree structure is not feasible - I need both navigability directions.
To solve this issue, I would rather use xlink to reference the xml objects instead of copying them in cascade. Is this solution pertinent? Is it possible to do that with JAXB marshaller? How?
You can implement an XLink approach in JAXB using an XmlAdapter. Below are links to various similar answers.
Serialize a JAXB object via its ID?
Using JAXB to cross reference XmlIDs from two XML files
Can JAXB marshal by containment at first then marshal by #XmlIDREF for subsequent references?
I lead the EclipseLink JAXB (MOXy) implementation, and we have the #XmlInverseReference extension for mapping bidirectional relationship that you may be interested in:
http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html
Dude, you can note in one of the entity the annotation #XmlTransient, so when unmarch, it will not complain about the cycle problem.
But with thius workaround after unmarch the xml you will have to populate the atribute with the #XmlTransient.
I was reading some paper and find this. You can set #XmlTransient and use the callback method to do something after the unmarch. So you can set the parent to you child.
public void afterUnmarshal(Unmarshaller u, Object parent) {
this.pessoa = (Pessoa) parent; }

use Jackson JSON library with JAXB annotations

I'm trying to enable JAXB annotation support within my RESTEasy based web application and this article was suggested to me (http://wiki.fasterxml.com/JacksonJAXBAnnotations). I'm able to get the jackson-xc.jar but I don't see how to register the annotation introspector. Currently, RESTEasy automatically serializes my JSON responses, where would the below fit? Currently, RESTeasy serializes the JSON object automatically.
Include jackson-xc jar, which contains org.codehaus.jackson.xc.JaxbAnnotationIntrospector
Register this annotation introspector
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
// make deserializer use JAXB annotations (only)
mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
// make serializer use JAXB annotations (only)
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
What you need to figure out is how to make RESTeasy use ObjectMapper you have configured. JAX-RS offers generic way to do this via providers, but there may be simpler way(s) to do this with RESTeasy.
Some JAX-RS implementations also register JAXB annotation introspector by default.
Last thing: are you sure you need to use JAXB annotations? While Jackson can use them, preferable method is to either just use basic naming convention for discovery and jackson's own annotations in case where overrides are needed. Problem with JAXB is that it is xml-specific so there is little bit of impedance when used with JSON.
Thanks for the reply, I'll take a look at your suggestions...
The reason why we want to stick with the jaxb annotations is that the #XmlRootElement(name="userResponse") annotation allows an extra wrapping element that we want in our JSON responses. See example below:
We want to have {"response":{"userResponse":[{"user":{"businessRegion":"US","firstName":"joe","language":"en","lastName":"smith"}}}]} instead of what Jackson currently outputs, {"response":[{"user":{"businessRegion":"US","firstName":"joe","language":"en","lastName":"smith"}}]}. Is there anyway to mimic the #XmlRootElement in Jackson without adding an additional wrapper class?
User class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name="userResponse")
public class UserResponse extends AbstractResponse{
private Users user;

How do I make more complex XSDs using JAXB?

Using JAXB I can create an XSD using code like:
JAXBContext ctx = JAXBContext.newInstance(classes);
ctx.generateSchema(new MySchemaOutputResolver());
That makes a goods XSD describing the structure of all the JAXB objects in the list of classes I pass in, however, I can't figure out how to add other types of XSD restrictions like minOccurs, maxOccurs, pattern, etc.
Is it possible to add annotations which indicate that additional information so that the XSD will include it?
You can use the #XmlElement(required = true) annotation to make an item required. Similar annotations exist for repetition, etc.
See here for annotation classes, the Javadoc has details.

Resources