Question

In my current project we use JAXB beans for Hibernate and for sending data out in XML and JSON via a [REST API][1]. We want to be able to arbitrarily include any JAXB bean inside a Result object we use as a REST VO (e.g. as an <xsd:any> element) without having a separate marshalling method for each different type that could be included. It seems the easiest way to get this to work is to have the @XmlRootElement annotation on each bean.

  1. Is this a good approach to "seamless" marshalling?
  2. Are there consequences to doing this?

We are using Java 1.6, EclipseLink/MOXy for jaxb, Jersey 1.x for REST, and Tomcat app server. Our beans are generated from XML Schema files with xjc, and using annox to add the annotation at generation time.

[1]: In the ideal world the data objects would be separate from the domain objects but this is a small project on a small team, so the isolation between database and api is not necessary

Was it helpful?

Solution

Are there any concerns with adding @XmlRootElement to every JAXB bean?

No.


We want to be able to arbitrarily include any JAXB bean inside a Result

A generic Result object with a field/property annotated with @XmlAnyElement(lax=true) is a good way to create a generic message where arbitrary payload objects annotated with @XmlRootElement can be used.


we use as a REST VO (e.g. as an element) without having a separate marshalling method for each different type that could be included.

My concern here is that the data you get back won't look the way you want it to. SOAP has an envelope message format where the data is in the body. Generally REST does not have this. When I access a RESTful endpoint I generally don't expect some sort of envelope wrapping the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top