users@jaxb.java.net

(no subject)

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Tue, 11 Jan 2011 11:05:08 +0100

Hello Frederic,

please see inline

On 1/11/11 9:38 AM, Frederic Bergeron wrote:
> Hi,
>
> I'm using JAXB in Jersey RESTful framework. I have a POJO class used for data exchange and I use it to serialize and deserialize its XML representation. It works fine so far. I have the following problem though.
>
> First, let's look at my class:
>
> package ca.licef;
>
> import javax.xml.bind.annotation.XmlType;
> import javax.xml.bind.annotation.XmlRootElement;
>
> @XmlType( propOrder = { "identifier", "description", "url", "contactEmail" } )
> @XmlRootElement
> public class Registry {
>
> public Registry () {
> }
>
> public Registry( String identifier, String description, String url, String contactEmail ) {
> this.identifier = identifier;
> this.description = description;
> this.url = url;
> this.contactEmail = contactEmail;
> }
>
> public String getIdentifier() {
> return( identifier );
> }
>
> public void setIdentifier( String identifier ) {
> this.identifier = identifier;
> }
>
> public String getDescription() {
> return( description );
> }
>
> public void setDescription( String description ) {
> this.description = description;
> }
>
> public String getUrl() {
> return( url );
> }
>
> public void setUrl( String url ) {
> this.url = url;
> }
>
> public String getContactEmail() {
> return( contactEmail );
> }
>
> public void setContactEmail( String contactEmail ) {
> this.contactEmail = contactEmail;
> }
>
> public boolean equals( Object obj ) {
> if( obj == this )
> return( true );
> if( !( obj instanceof Registry) )
> return( false );
> Registry registry = (Registry)obj;
> return( identifier != null&& identifier.equals( registry.getIdentifier() ) );
> }
>
> public int hashCode() {
> return( identifier.hashCode() );
> }
>
> public String toString() {
> StringBuilder str = new StringBuilder();
> str.append( identifier ).append( "," );
> str.append( url );
> return( str.toString() );
> }
>
> private String identifier;
> private String url;
> private String description;
> private String contactEmail;
>
> }
>
> When I return a List containing several instances of Registry objects, the generated XML looks like this:
>
> <registries>
> <registry>
> <identifier>LicefRegistry</identifier>
> <description>Registry of Licef's repositories.</description>
> <url>http://registry.licef.ca:8080</url>
> <contactEmail>frederic.bergeron_at_licef.ca</contactEmail>
> </registry>
> <registry>
> <identifier>Another dummy registry</identifier>
> <description>Experimental registry used for fun.</description>
> <url>http://fun.licef.ca/registry:8080</url>
> <contactEmail>technical_at_fun.licef.ca</contactEmail>
> </registry>
> </registries>
>
> This looks great, especially since JAXB is smart enough to pluralize the word "registry" appropriately as "registries" for the collection wrapper element.
>
> Now, I would like to rename my class from Registry to RegistryResource. Doing so will have an impact on the generated XML. So according to the documentation, I use additional annotations to declare the name of the element like this:
>
> package ca.licef.resource;
>
> import javax.xml.bind.annotation.XmlType;
> import javax.xml.bind.annotation.XmlRootElement;
>
> @XmlRootElement(name="registry")
> @XmlType( name="registry", propOrder = { "identifier", "description", "url", "contactEmail" } )
> public class RegistryResource {
>
> public RegistryResource () {
> }
>
> public RegistryResource( String identifier, String description, String url, String contactEmail ) {
> this.identifier = identifier;
> this.description = description;
> this.url = url;
> this.contactEmail = contactEmail;
> }
>
> ... the rest of the code is identical to previous version ...
>
>
> }
>
> Now, when I ask for the list of registries, the generated XML looks like this:
>
> <registryResourcs>
> <registry>
> <identifier>LicefRegistry</identifier>
> <description>Registry of Licef's repositories.</description>
> <url>http://registry.licef.ca:8080</url>
> <contactEmail>frederic.bergeron_at_licef.ca</contactEmail>
> </registry>
> <registry>
> <identifier>Another dummy registry</identifier>
> <description>Experimental registry used for fun.</description>
> <url>http://fun.licef.ca/registry:8080</url>
> <contactEmail>technical_at_fun.licef.ca</contactEmail>
> </registry>
> </registryResourcs>
>
> As you can see, the collection wrapper element name is now "registryResources" instead "registries". How can I have it named "registries"? Do I need extra annotations? Or maybe an additional class?
additional class is definite solution, but might not be needed in this
case..

to be exact, this is not JAXB issue, Jersey has its own mechanism to
determine and generate "wrapping" element. Firstly, XmlRootElement
annotation wasn't processed - I fixed it some time ago, but there is a
property which you need to turn on because of backwards compatibility.

You need to use FEATURE_XMLROOTELEMENT_PROCESSING, see [1].
> I don't know if it's important but here is the Jersey code that produces the list of registries:
>
> @GET
> @Produces( MediaType.APPLICATION_XML )
> public List<RegistryResource> getRegistryList() {
> List<RegistryResource> registryList = new ArrayList<RegistryResource>();
> for( Iterator it = registries.values().iterator(); it.hasNext(); ) {
> RegistryResource registry = (RegistryResource)it.next();
> registryList.add( registry );
> }
> return( registryList );
> }
>
> I'm also wondering... What if I wanted the collection element to have a completely different name? For example, instead of "registries", what if I want to have "registryList" or "registryCollection"? Do I need to make another POJO class for the collection in such case?

yes and no; you can create your own message body writer for this case,
which should be cleaner than modifying your generated classes. Again,
this is more Jersey related question, so next time, please use
users_at_jersey.java.net list.

Hope it helps,
Pavel


[1]
http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/core/util/FeaturesAndProperties.html#FEATURE_XMLROOTELEMENT_PROCESSING


> I hope someone knows the solution.
>
> Regards,
>
> Frederic Bergeron
> Licef
>
>
>
>
>
>