Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

jaxb - How to customize namespace prefixes on Jersey(JAX-WS)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...