RestEasy的/ JAXB;如何避免将名称空间添加到< any>中的元素标签? (JAXB中的List< Element>) [英] RESTeasy/JAXB; How do I avoid a namespace being added to an Element in an <any> tag? (List<Element> in JAXB)

查看:62
本文介绍了RestEasy的/ JAXB;如何避免将名称空间添加到< any>中的元素标签? (JAXB中的List< Element>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将尽可能地简化我的课程和输出,但基本上我想要的是我想要添加 org.w3c.dom.Element (在这种情况下表示一个原子链接)到我正在返回的JAXB对象。 JAXB类看起来像:

I'm going to simplify my classes and output as best I can here, but basically what I'm after is I want to add an org.w3c.dom.Element (representing an atom link in this case) to a JAXB object I'm returning. The JAXB Class looks something like:

import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
import org.w3c.dom.Element;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "People", namespace = "main", propOrder = {
    "any",
    "persons"
})
public class People {
    @XmlAnyElement
    protected List<Element> any;
    @XmlElement(name = "person", namespace = "main")
    protected List<Person> persons;
    [...]
}

我正在使用元素创建元素我创建的模板如下:

I'm creating the Element using a template I create like this:

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class ElementGen {
    public Element getTemplate() throws DOMException, SAXException, ParserConfigurationException {
        final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final Schema schema = sf.newSchema(new StreamSource(
                Thread.currentThread().getContextClassLoader().getResourceAsStream(ATOM_XSD)));
        final DocumentBuilderFactory docBuilder = DocumentBuilderFactory.newInstance();
        docBuilder.setSchema(schema);
        final Document doc = docBuilder.newDocumentBuilder().newDocument();
        linkTemplate = doc.createElementNS(ATOM_NAMESPACE, ATOM_LINK);
        return linkTemplate;
    }
}

(实际上这不是班级的样子,我我只是试图让它尽可能简单地编译一些东西来测试它而不需要所有的外部混乱。)

(That's not actually what the class looks like, I'm just trying to make it as easy as possible to compile something to test it without all the external mess).

然后我使用 linkTemplate.cloneNode(false);

现在这一切都在于它返回xml,但奇怪的是我得到的xml back附加了额外的名称空间:

Now this all works in that it returns xml, but the odd thing is that the xml I get back has additional namespaces attached:

<atom:link xmlns:ns3="main" xmlns="" href="href" rel="rel"/>

如果我添加 linkTemplate.setAttribute(xmlns,null); xmlns:ns3命名空间消失了,我得到:

If I add linkTemplate.setAttribute("xmlns", null); the "xmlns:ns3" namespace disappears and I get:

<atom:link xmlns="" href="href" rel="rel"/>

但似乎无法删除xmlns =。我是以错误的方式创建元素吗?或者其他可能出错的地方?我完全不知道为什么它会添加这些,所以任何帮助/解释都会受到赞赏。

But there seems to be no way of removing that xmlns="". Am I creating the Element in the wrong way? Or perhaps something else is going wrong? I'm rather at a loss as to why it's adding these at all so any help/explanation would be appreciated.

编辑:我认为它必须与命名空间相关我用来生成元素的文档,但我不知道如何解决它。有没有办法在文档上设置(XML)targetNamespace?

I believe it must be related to the namespace of the document I use for generating the Element, but I'm not sure how to fix it. Is there any way of setting the (XML) targetNamespace on the document?

编辑2:我不确定它是否添加了对任何人有用的东西,但更多的实验我发现 linkTemplate.setAttribute(xmlns:+ anything,null); 具有创建 xmlns的链接的效果:[anything] = 并删除本来会生成的其他任何内容。

Edit 2: I'm not sure if it adds anything useful for anyone, but with more experimenting I found that linkTemplate.setAttribute("xmlns:" + anything, null); has the effect of creating a link with xmlns:[anything]="" and removing any others that would have otherwise been generated.

编辑3:用于生成JAXB对象的xsd的相关位是:

Edit 3: The relevant bits of the xsd used to generate the JAXB objects is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0"
    xmlns="main"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:atom="http://www.w3.org/2005/Atom"
    targetNamespace="main"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

<xs:complexType name="People">
    <xs:sequence>
        <xs:any namespace="##other" processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="person" type="Person" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    [attributes]
</xs:complexType>
[other types etc.]


推荐答案

自这里没有任何建议对我有用我决定走另一条路。最后,我最终覆盖了监听器RESTEasy使用我自己的监听器添加到Marshaller。然后,此侦听器调用RESTEasy侦听器(如果存在),然后在RESTServiceDiscovery字段上手动添加链接(您必须使用反射获取此字段,并在获取对象之前关闭使用field.setAccessible(true)的访问检查) 。

Since none of the suggestions here have worked for me I've decided to go a different route. In the end I ended up overwriting the Listener RESTEasy adds to the Marshaller with a Listener of my own. This Listener then calls the RESTEasy listener (if it was present), before adding the links manually on the RESTServiceDiscovery field (you have to get this field using reflection, and turn off access checking with field.setAccessible(true) before getting the object).

这篇关于RestEasy的/ JAXB;如何避免将名称空间添加到&lt; any&gt;中的元素标签? (JAXB中的List&lt; Element&gt;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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