views:

36

answers:

1

Hi,

when serializing my resources on Jersey, I want to use namespaces in some cases.

Is there any way to customize the namespace prefixes on jersey?

Default:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order xmlns:ns2="http://www.w3.org/2005/Atom">
   <price>123</price>
   <ns2:link rel="duh" href="/abc/123"/>
   <ns2:link rel="abc" href="/def/234"/>
</order>

I want something like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order xmlns:atom="http://www.w3.org/2005/Atom">
   <price>123</price>
   <atom:link rel="duh" href="/abc/123"/>
   <atom:link rel="abc" href="/def/234"/>
</order>

Thanks, Lucas

A: 

If you use the MOXy JAXB implementation you can control your prefixes using the @XmlSchema package level annotation:

@javax.xml.bind.annotation.XmlSchema(  
    xmlns = {  
          @javax.xml.bind.annotation.XmlNs(prefix = "atom", namespaceURI = "http://www.w3.org/2005/Atom")  
            })  
    package org.example.domain;  

To use MOXy JAXB you need to have a file named jaxb.properties in with your model classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

For an example of using MOXy with Jersey see:

Blaise Doughan
Sounds nice =)So there is no way to do this with the standard JAXB?
Lucas Cavalcanti
The JAXB standard (JSR-222) does not specify a way to do this. The MOXy JAXB implementation leverages the standard annotation to get the desired behaviour. Metro JAXB (the reference implemenation) offers an extension called NamespacePrefixMapper to achieve this result: http://blogs.sun.com/enterprisetechtips/entry/customizing_jaxb
Blaise Doughan
Is it possible to change JAXB implementation on Grizzly? The jaxb.properties doesn't work, nor init-params.I'll have to deploy my app on an application server?
Lucas Cavalcanti
It's working now... maven was not exporting the jaxb.properties into classpath. Thanks =)
Lucas Cavalcanti