users@jersey.java.net

Namspace prefixes

From: Tim McCune <tim_at_mccune.name>
Date: Thu, 17 Jul 2008 17:21:16 -0700

Hi. I'm using the latest release of Jersey, and returning JAXB-bound
objects that are getting marshalled to XML by Jersey. When the marshalling
happens, JAXB seems to be using some arbitrary namespace prefixes (ns1, ns2,
etc.) for the elements, instead of the prefixes that I've declared in
package-info.java.

HelloWorld.java:
@XmlRootElement
public class HelloWorld {
    @XmlAttribute
    public String getMessage() {
        return "Hello World!";
    }
    @XmlAttribute(namespace="http://otherNamespace")
    public Date getCreated() {
        return new Date();
    }
}

package-info.java:
@XmlSchema (
    namespace="http://myNamespace",
    xmlns={
        @XmlNs(prefix="foo", namespaceURI="http://otherNamespace"),
        @XmlNs(prefix="bar", namespaceURI="http://myNamespace")
    }
)
package mypkg;

HelloWorldResource.java:
@Path("/helloWorld")
public class HelloWorldResource {
    @GET
    @ProduceMime({"text/xml"})
    public HelloWorld sayHello() {
        return new HelloWorld();
    }
}

The XML that I get looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:helloWorld xmlns:ns1="http://otherNamespace"
xmlns:ns2="http://myNamespace" message="Hello World!"
ns1:created="2008-7-17"/>

I checked out the "namespace-prefix" example in the JAXB-RI, and they seem
to be setting a property on the JAXB Marshaller:

marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",new
NamespacePrefixMapperImpl());

However, I don't see any way to do this in Jersey, short of writing my own
XMLRootElementProvider, which I tried, only to get a vague
"PropertyException" from the marshaller when I try to set that property. So
I thought I'd ping the list. This seems like a very common thing to want to
do, and it definitely shouldn't be this hard! :) Any idea what I might be
doing wrong and/or what is the proper way to get Jersey/JAXB to use the
prefixes that I want it to?

Thanks.

--Tim