Webservice客户端调用webservice客户端 - java

时间:2014-07-14 15:59:54

标签: java soap xsd wsdl jax-ws

我必须创建一个java客户端来执行web服务。我有一个包含整个SOAP请求(包络,标题,正文)的XML。

如何通过传递包含soap请求的xml文件来编写执行webservice的java代码?

我尝试了很多但是找不到这样做的样本

服务器上的Web服务位于SOAP 1.1上,内容类型为#text; xml'

例如 wsdlLocation =" HTTP://本地主机:8080 / HelloService的/你好WSDL"

webservice没有输入参数,这就是数据必须作为soap请求完全传递的原因。传递的数据采用xml的形式。

示例SOAP请求xml文件示例(Sample.xml)

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<putTxlife1203Info xmlns="http://www.openuri.org/">
<TXLife>

</TXLife>
</putTxlife1203Info>
</env:Body>
</env:Envelope>

如果有人可以提供一个很棒的样本,真的很棒

3 个答案:

答案 0 :(得分:3)

import javax.xml.soap.*;

public String callTestService(String soapRequestXml, String url) throws Exception {
    // Create SOAP Connection
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();

    SOAPMessage soapRequest = MessageFactory.newInstance().createMessage(new MimeHeaders(),
            new ByteArrayInputStream(soapRequestXml.getBytes()));

    // Send SOAP Message to SOAP Server
    SOAPMessage soapResponse = soapConnection.call(soapRequest, url);

    ByteArrayOutputStream soapResponseBaos = new ByteArrayOutputStream();
    soapResponse.writeTo(soapResponseBaos);
    String soapResponseXml = soapResponseBaos.toString();

    return soapResponseXml;
}

答案 1 :(得分:1)

如果你有完整的xml,SOAP请求保存在文件中,需要直接发送(我猜测试),那么只需使用普通的http客户端,然后用它做POST请求。 以下是一些如何操作的示例:

Sending HTTP Post request with SOAP action using org.apache.http

答案 2 :(得分:0)

要生成客户端服务方法和内容,请使用wsimport工具,如下所示:

wsimport -keep http://localhost:8080/helloservice/hello?wsdl

资料来源:http://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example/如果您真的(我的意思是真的真的)想要手工完成,那么说明也就在那里。我希望生成而非创建这样的事情,让我们保持简单。

然后用以下内容创建客户端:

package com.mkyong.client;

import com.mkyong.ws.HelloWorld;
import com.mkyong.ws.HelloWorldImplService;

public class HelloWorldClient{

    public static void main(String[] args) {

    HelloWorldImplService helloService = new HelloWorldImplService();
    HelloWorld hello = helloService.getHelloWorldImplPort();

    System.out.println(hello.getHelloWorldAsString("mkyong"));

   }

}

这是上面链接的直接引用,方法名称可能会因服务器端实际服务所遵循的教程而有所不同。