重用信封名称空间创建 SOAP 主体

问题描述 投票:0回答:0

我需要通过以下方式创建 SOAP 消息(注意

example:SomeCommand
):

<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:example="example.com">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <example:SomeCommand>
            <Field1>field1</Field1>
            <Field2>field2</Field2>
        </example:SomeCommand>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我有以下代码:

public String marshal() throws Exception {
    SomeCommand someCommand = new SomeCommand("field1", "field2");

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    Document document = documentBuilderFactory.newDocumentBuilder().newDocument();

    Marshaller marshaller = JAXBContext.newInstance(SomeCommand.class).createMarshaller();
    marshaller.marshal(someCommand, document);

    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    soapMessage.getSOAPPart().getEnvelope().addNamespaceDeclaration( "example", "example.com");
    soapMessage.getSOAPBody().addDocument(document);

    OutputStream outputStream = new ByteArrayOutputStream();
    soapMessage.writeTo(outputStream);

    return outputStream.toString();
}

SomeCommand
在哪里:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(namespace = "example.com", name = "SomeCommand")
public class SomeCommand {

    @XmlElement(name = "Field1", required = true)
    private String field1;

    @XmlElement(name = "Field2", required = true)
    private String field2;

    public SomeCommand(){}

    public SomeCommand(String field1, String field2){
        this.field1 = field1;
        this.field2 = field2;
    }
}

但结果是:

<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:example="example.com">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <ns2:SomeCommand
            xmlns:ns2="example.com">
            <Field1>field1</Field1>
            <Field2>field2</Field2>
        </ns2:SomeCommand>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

有没有什么方法可以在 SOAP 消息的正文中附加一个文档,重用现有的命名空间 (

example
) 而不是重新定义它们?

提前致谢。

xml soap jaxb jax-rs xml-namespaces
© www.soinside.com 2019 - 2024. All rights reserved.