从Java对象生成SOAP请求

时间:2016-02-18 11:34:19

标签: soap jaxb

我需要从Java中的请求对象创建一个Soap请求。需要的是:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <tem:tag1>
        <tem:tag2>
        <MyDataSet>
            <!-- more elements within-->
        </MyDataSet>
        <tem:tag1>
        <tem:tag2>
    </SOAP-ENV:Body>
    <atom/>
</SOAP-ENV:Envelope>

然而,我得到的是:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <MyDataSet>
            <!-- more elements within-->
        </MyDataSet>
    </SOAP-ENV:Body>
    <atom/>
</SOAP-ENV:Envelope>

有人可以告诉我如何在soap请求中添加<tem:tag1><tem:tag2>吗?这是我到目前为止编写的代码:

 public static void main(String[] args) throws Exception
    {
        MyRequest request = new MyRequest();
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        Marshaller marshaller = JAXBContext.newInstance(MyRequest.class).createMarshaller();
        marshaller.marshal(request, document);
        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
        soapMessage.getSOAPPart().getEnvelope().addNamespaceDeclaration("tem", "http://tempuri.org/");
        soapMessage.getSOAPBody().addDocument(document);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        soapMessage.writeTo(outputStream);
        String output = new String(outputStream.toByteArray());
        System.out.println(output);
    }

1 个答案:

答案 0 :(得分:-1)

假设Web服务已发布WSDL,我建议您不要滚动自己的客户端代码,而是使用wsimport(Java JDK中包含的实用程序)生成它。可以找到一篇关于如何使用它的文章here

相关问题