users@jaxb.java.net

(no subject)

From: Frederic Bergeron <FBergeron_at_rocketmail.com>
Date: Tue, 11 Jan 2011 00:38:56 -0800 (PST)

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?

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?

I hope someone knows the solution.

Regards,

Frederic Bergeron
Licef