Web services jaxb向元素添加名称空间

Web services jaxb向元素添加名称空间,web-services,soap,jaxb,wsdl,Web Services,Soap,Jaxb,Wsdl,根据我的任务,我必须调用SOAP服务。因此,我使用xjc从wsdl生成了java类。但我在调用SOAP服务时遇到了一个问题。我的应用程序生成此请求: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/><soap:Bo

根据我的任务,我必须调用SOAP服务。因此,我使用xjc从wsdl生成了java类。但我在调用SOAP服务时遇到了一个问题。我的应用程序生成此请求:

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/><soap:Body>
<typ:SendMessage xmlns:ns4="http://test.user.kz/UserInfo" xmlns:q1="http://test.user.kz/CustomerInfo" xmlns:typ="http://test.user.kz/MyChannel/v1/Types">
    <request>
        <requestInfo>
            <messageId>26e96b11-8f82-421e-829a</messageId>
        </requestInfo>
        <requestData>
            <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="q1:PaymentPackageRequestType">
                <q1:methodName>testMethod</q1:methodName>
            </data>
        </requestData>
    </request>
</typ:SendMessage></soap:Body></soap:Envelope>
如何在数据标记中指定名称空间? 这是我当前的包裹信息:

@javax.xml.bind.annotation.XmlSchema(
        xmlns = {
            @javax.xml.bind.annotation.XmlNs(prefix = "typ",
                    namespaceURI = "http://test.user.kz/MyChannel/v1/Types"),
            @javax.xml.bind.annotation.XmlNs(prefix = "q1",
                    namespaceURI = "http://test.user.kz/CustomerInfo")
        },
   elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED,
   attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNSET
)
package kz.uni.gen;

因此,我无法添加名称空间xmlns:q1=”http://test.user.kz/CustomerInfo“到SOAP请求中的数据标记。如何添加此命名空间声明或从SendMessage标记移动命名空间声明?

因此使用JAXB是不可能的。因此,我在必需元素中手动添加了名称空间。这是完整的代码片段:

Document document = null;
        try {
            document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Marshaller marshaller = JAXBContext.newInstance(SendMessage.class).createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", new CharacterEscapeHandler() {
                @Override
                public void escape(char[] buf, int start, int len, boolean b, Writer out) throws IOException {
                    out.write(buf, start, len);
                }
            });
            QName name = new QName(NAMESPACE_URI, SendMessage.class.getSimpleName());
            JAXBElement<SendMessage> root = new JAXBElement<SendMessage>(name, SendMessage.class, from);
            StringWriter writer1 = new StringWriter();
            marshaller.marshal(root, writer1);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            Element node = dbf
                    .newDocumentBuilder()
                    .parse(new ByteArrayInputStream(writer1.toString().getBytes(StandardCharsets.ISO_8859_1)))
                    .getDocumentElement();
            Attr attr1 = document.createAttribute("xmlns:q1");
            attr1.setValue("http://test.user.kz/CustomerInfo");
            node.getElementsByTagName("data").item(0).getAttributes().setNamedItem(node.getOwnerDocument().importNode(attr1, true));
            return node;
        } catch (Exception e) {
            throw new Exception("Unable to transform POJO to XML SOAP message ", e);

        }
单据单据=null;
试一试{
document=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Marshaller Marshaller=JAXBContext.newInstance(SendMessage.class).createMarshaller();
setProperty(marshaller.JAXB_编码,“UTF-8”);
setProperty(marshaller.JAXB_格式的_输出,Boolean.TRUE);
setProperty(“com.sun.xml.bind.characterEscapeHandler”,新的characterEscapeHandler(){
@凌驾
公共void转义(char[]buf,int start,int len,boolean b,Writer out)抛出IOException{
输出。写入(buf、start、len);
}
});
QName name=新的QName(名称空间_URI,SendMessage.class.getSimpleName());
JAXBElement root=newjaxbelement(名称,sendmages.class,from);
StringWriter writer1=新的StringWriter();
marshaller.marshall(根,writer1);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
元素节点=dbf
.newDocumentBuilder()
.parse(新的ByteArrayInputStream(writer1.toString().getBytes(StandardCharsets.ISO_8859_1)))
.getDocumentElement();
Attr attr1=document.createAttribute(“xmlns:q1”);
属性1.设置值(“http://test.user.kz/CustomerInfo");
node.getElementsByTagName(“数据”).item(0.getAttributes().setNamedItem(node.getOwnerDocument().importNode(attr1,true));
返回节点;
}捕获(例外e){
抛出新异常(“无法将POJO转换为XML SOAP消息”,e);
}
@javax.xml.bind.annotation.XmlSchema(
        xmlns = {
            @javax.xml.bind.annotation.XmlNs(prefix = "typ",
                    namespaceURI = "http://test.user.kz/MyChannel/v1/Types"),
            @javax.xml.bind.annotation.XmlNs(prefix = "q1",
                    namespaceURI = "http://test.user.kz/CustomerInfo")
        },
   elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED,
   attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNSET
)
package kz.uni.gen;
Document document = null;
        try {
            document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Marshaller marshaller = JAXBContext.newInstance(SendMessage.class).createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", new CharacterEscapeHandler() {
                @Override
                public void escape(char[] buf, int start, int len, boolean b, Writer out) throws IOException {
                    out.write(buf, start, len);
                }
            });
            QName name = new QName(NAMESPACE_URI, SendMessage.class.getSimpleName());
            JAXBElement<SendMessage> root = new JAXBElement<SendMessage>(name, SendMessage.class, from);
            StringWriter writer1 = new StringWriter();
            marshaller.marshal(root, writer1);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            Element node = dbf
                    .newDocumentBuilder()
                    .parse(new ByteArrayInputStream(writer1.toString().getBytes(StandardCharsets.ISO_8859_1)))
                    .getDocumentElement();
            Attr attr1 = document.createAttribute("xmlns:q1");
            attr1.setValue("http://test.user.kz/CustomerInfo");
            node.getElementsByTagName("data").item(0).getAttributes().setNamedItem(node.getOwnerDocument().importNode(attr1, true));
            return node;
        } catch (Exception e) {
            throw new Exception("Unable to transform POJO to XML SOAP message ", e);

        }