使用 Jersey、jaxb 和 jaxb 时如何设置 xml 命名空间jax-rs

发布于 2024-09-13 21:56:00 字数 49 浏览 3 评论 0原文

使用 Jersey、jaxb 和 jaxb 时如何设置 xml 命名空间jax-rs

How do I set the xml namespace when using Jersey, jaxb & jax-rs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仲春光 2024-09-20 21:56:00

这一切都是使用 JAXB 注释完成的。以下几点涉及您的域模型。

架构级别

您可以使用 @XmlSchema 包级别注释指定架构级别命名空间信息:

@XmlSchema(namespace = "http://www.example.org",
           elementFormDefault = XmlNsForm.QUALIFIED)
package org.example;

import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;

上述注释利用 elementFormDefault 将所有元素的命名空间默认为“http://www.example.org"。

类型级别

您可以使用@XmlType注释在类型级别覆盖命名空间:

@XmlType(namespace="http://www.example.org/foo")

属性/字段级别

和/或者您可以在注释本身上指定命名空间信息:

  • @XmlAttribute(命名空间=“http://www.example.org/bar”)
  • @XmlElement(命名空间=“http://www.example.org/bar”)
  • @XmlElementWrapper(命名空间=“http://www.example.org” ) /bar")
  • @XmlRootElement(namespace="http://www.example.org/bar")

示例

我有一篇博客文章通过示例演示了这些概念:

This is all done using JAXB annotations. The points below refer to your domain model.

Schema Level

You can specify schema level namespace information using the @XmlSchema package level annotation:

@XmlSchema(namespace = "http://www.example.org",
           elementFormDefault = XmlNsForm.QUALIFIED)
package org.example;

import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;

The above annotation leveraging elementFormDefault will default the namespace of all elements to "http://www.example.org".

Type Level

You can override namespaces at the type level using the @XmlType annotation:

@XmlType(namespace="http://www.example.org/foo")

Property/Field Level

And/or you can specify namespace information on the annotations themselves:

  • @XmlAttribute(namespace="http://www.example.org/bar")
  • @XmlElement(namespace="http://www.example.org/bar")
  • @XmlElementWrapper(namespace="http://www.example.org/bar")
  • @XmlRootElement(namespace="http://www.example.org/bar")

Example

I have a blog post that demonstrates these concepts with an example:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文