users@jersey.java.net

Re: [Jersey] Namspace prefixes

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 23 Jul 2008 10:46:28 +0200

Hi Tim,

I am wondering why JAXB itself does not detect the @XMLSchema
declaration rather than you having to explicitly declare the prefixes
from that annotation.

Perhaps you need to create a JAXB context using the package name? or
pass the package-info as well as the other JAXB classes?

Paul.

Tim McCune wrote:
> 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

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109