如何在Java中使用SOAP Web服务

Oyu*_*giK 14 java xml soap java-ee

有人可以帮我一些关于如何在Java中使用Web服务WSDL的链接和其他方面吗?

con*_*ner 8

我会使用CXF, 你也可以想到AXIS 2.

最好的方法是使用JAX RS参考这个例子

Example:

wsimport -p stockquote http://stockquote.xyz/quote?wsdl

This will generate the Java artifacts and compile them by importing the http://stockquote.xyz/quote?wsdl.
Run Code Online (Sandbox Code Playgroud)

一世

  • 我认为我的工作压力很大.如果导入WSDL文件确定您是活着还是已死,那么您必须将其弄糟.大声笑 (7认同)

RAS*_*RAS 6

使用基于WSDL创建的Stub或Java类来使用SOAP Web服务有很多选择.但是,如果有人想这样做没有任何创建Java类,这个文章是非常有帮助的.文章的代码片段:

public String someMethod() throws MalformedURLException, IOException {

//Code to make a webservice HTTP request
String responseString = "";
String outputString = "";
String wsURL = "<Endpoint of the webservice to be consumed>";
URL url = new URL(wsURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)connection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
String xmlInput = "entire SOAP Request";

byte[] buffer = new byte[xmlInput.length()];
buffer = xmlInput.getBytes();
bout.write(buffer);
byte[] b = bout.toByteArray();
String SOAPAction = "<SOAP action of the webservice to be consumed>";
// Set the appropriate HTTP parameters.
httpConn.setRequestProperty("Content-Length",
String.valueOf(b.length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction", SOAPAction);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
//Write the content of the request to the outputstream of the HTTP Connection.
out.write(b);
out.close();
//Ready with sending the request.

//Read the response.
InputStreamReader isr = null;
if (httpConn.getResponseCode() == 200) {
  isr = new InputStreamReader(httpConn.getInputStream());
} else {
  isr = new InputStreamReader(httpConn.getErrorStream());
}

BufferedReader in = new BufferedReader(isr);

//Write the SOAP message response to a String.
while ((responseString = in.readLine()) != null) {
outputString = outputString + responseString;
}
//Parse the String output to a org.w3c.dom.Document and be able to reach every node with the org.w3c.dom API.
Document document = parseXmlFile(outputString); // Write a separate method to parse the xml input.
NodeList nodeLst = document.getElementsByTagName("<TagName of the element to be retrieved>");
String elementValue = nodeLst.item(0).getTextContent();
System.out.println(elementValue);

//Write the SOAP message formatted to the console.
String formattedSOAPResponse = formatXML(outputString); // Write a separate method to format the XML input.
System.out.println(formattedSOAPResponse);
return elementValue;
}
Run Code Online (Sandbox Code Playgroud)

对于那些在使用SOAP API的同时寻找类似文件上传解决方案的人,请参考这篇文章:如何在SOAP POST请求中附加文件(pdf,jpg等)?


Mac*_*gan 5

由于有些建议,您可以使用apache或jax-ws。您还可以使用从WSDL生成代码的工具(例如ws-import),但我认为使用Web服务的最佳方法是创建动态客户端,并仅调用所需的操作,而不需要wsdl的所有操作。您可以通过创建动态客户端来做到这一点:示例代码:

String endpointUrl = ...;

QName serviceName = new QName("http://com/ibm/was/wssample/echo/",
 "EchoService");
QName portName = new QName("http://com/ibm/was/wssample/echo/",
 "EchoServicePort");

/** Create a service and add at least one port to it. **/ 
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointUrl);

/** Create a Dispatch instance from a service.**/ 
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, 
SOAPMessage.class, Service.Mode.MESSAGE);

/** Create SOAPMessage request. **/
// compose a request message
MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);

// Create a message.  This example works with the SOAPPART.
SOAPMessage request = mf.createMessage();
SOAPPart part = request.getSOAPPart();

// Obtain the SOAPEnvelope and header and body elements.
SOAPEnvelope env = part.getEnvelope();
SOAPHeader header = env.getHeader();
SOAPBody body = env.getBody();

// Construct the message payload.
SOAPElement operation = body.addChildElement("invoke", "ns1",
 "http://com/ibm/was/wssample/echo/");
SOAPElement value = operation.addChildElement("arg0");
value.addTextNode("ping");
request.saveChanges();

/** Invoke the service endpoint. **/
SOAPMessage response = dispatch.invoke(request);

/** Process the response. **/
Run Code Online (Sandbox Code Playgroud)

  • @user755611我向您展示的代码没有导入任何类。它是动态客户端的 shell,您可以使用它直接连接到 wsdl 并执行您想要的操作。因此,在您的程序中,您将只有此代码和相应的代码,并为 wsdl 提供参数并执行所需的方法,而无需从 wsdl 导入任何类 (2认同)

Pop*_*rei 5

在这里您可以找到一个很好的教程,介绍如何通过 WSDL 创建和使用 SOAP 服务。长话短说,您需要从命令行调用wsimport工具(您可以在 jdk 中找到它),并使用 -s(.java 文件的源)-d(.class 文件的目标)和 wsdl 链接等参数。

$ wsimport -s "C:\workspace\soap\src\main\java\com\test\soap\ws" -d "C:\workspace\soap\target\classes\com\test\soap\ws" http://localhost:8855/soap/test?wsdl
Run Code Online (Sandbox Code Playgroud)

创建存根后,您可以非常简单地调用 Web 服务,例如:

TestHarnessService harnessService = new TestHarnessService();
ITestApi testApi = harnessService.getBasicHttpBindingITestApi();
testApi.resetLogMemoryTarget();
Run Code Online (Sandbox Code Playgroud)