I\'m using MOXy with Jersey to implement a RESTful API and want to return lists naturally for JSON and XML, by which I mean that the XML contains an element tag for the over
You could do the following to get the desired XML and JSON representations:
@XMLElementWrapper
Instead of:
@XmlRootElement(name="organisation")
@XmlAccessorType(XmlAccessType.FIELD)
public class ExternalOrganisation {
private String name;
private ExternalFacilities facilities;
private ExternalLocations locations;
...
}
You could do the following with @XmlElementWrapper
(see: http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html):
@XmlRootElement(name="organisation")
@XmlAccessorType(XmlAccessType.FIELD)
public class ExternalOrganisation {
private String name;
@XmlElementWrapper
@XmlElementRef
private List<ExternalFacility> facilities;
@XmlElementWrapper
@XmlElementRef
private List<ExternalLocation> locations;
...
}
By specifying the wrapper as array name property, MOXy will use the value from @XmlElementWrapper
as the JSON array name.
import java.util.*;
import javax.ws.rs.core.Application;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
public class YourApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>(1);
set.add(YourService.class);
return set;
}
@Override
public Set<Object> getSingletons() {
MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
moxyJsonProvider.setWrapperAsArrayName(true);
HashSet<Object> set = new HashSet<Object>(1);
set.add(moxyJsonProvider);
return set;
}
}
For More Information