如何在Jersey(JAX-WS)上自定义名称空间前缀 [英] How to customize namespace prefixes on Jersey(JAX-WS)

查看:95
本文介绍了如何在Jersey(JAX-WS)上自定义名称空间前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jersey上序列化我的资源时,我想在某些情况下使用命名空间。

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

有没有办法在jersey上自定义命名空间前缀?

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

默认值:


<?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>

我想要类似的东西:


<?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>

谢谢,
Lucas

Thanks, Lucas

推荐答案

如果您使用 MOXy JAXB实现你可以使用@XmlSchema包级别注释控制你的前缀:

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;  

要使用MOXy JAXB,您需要将一个名为jaxb.properties的文件与您的模型类一起使用以下条目:

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

有关在泽西使用MOXy的示例,请参阅:

For an example of using MOXy with Jersey see:

  • http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-35.html

这篇关于如何在Jersey(JAX-WS)上自定义名称空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆