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

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

问题描述

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

有没有办法自定义球衣上的命名空间前缀?

默认:

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

我想要类似的东西:

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

谢谢,卢卡斯

解决方案

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

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

要使用 MOXy JAXB,您需要在模型类中包含一个名为 jaxb.properties 的文件,其中包含以下条目:

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

有关在 Jersey 中使用 MOXy 的示例,请参阅:

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

解决方案

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:

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

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