我了解您的问题归结为如何从 Java 调用 SOAP (JAX-WS) Web 服务并获取其返回对象。在这种情况下,您有两种可能的方法:
- 通过Java类生成
wsimport
并使用它们;或者
- 创建一个 SOAP 客户端:
- 将服务的参数序列化为 XML;
- 通过 HTTP 操作调用 web 方法;和
- 将返回的 XML 响应解析回对象。
关于第一种方法(使用wsimport
):
wsimport
我看到您已经拥有服务(实体或其他)业务类,并且生成了一组全新的类(它们在某种程度上是您已经拥有的类的副本),这是一个事实。
不过,恐怕在这种情况下,您只能:
- 调整(编辑)
wsimport
生成的代码以使其使用您的业务类(这很困难,而且不知何故不值得 - 请记住,每次 WSDL 更改时,您都必须重新生成并重新调整代码);或者
- 放弃并使用
wsimport
生成的类。(在这个解决方案中,您的业务代码可以“使用”生成的类作为来自另一个架构层的服务。)
关于第二种方法(创建您的自定义 SOAP 客户端):
为了实施第二种方法,您必须:
- 拨打电话:
- 使用 SAAJ(SOAP with Attachments API for Java)框架(见下文,Java SE 1.6 或更高版本附带)进行调用;或者
- 您也可以通过
java.net.HttpUrlconnection
(和一些java.io
处理)来做到这一点。
- 将对象转换为 XML 并从 XML 转换回来:
- 使用诸如 JAXB 之类的 OXM(对象到 XML 映射)框架来序列化/反序列化 XML 从/到对象
- 或者,如果必须,手动创建/解析 XML(如果接收到的对象与发送的对象只有一点点不同,这可能是最好的解决方案)。
使用经典创建 SOAP 客户端java.net.HttpUrlConnection
并不难(但也不是那么简单),您可以在此链接中找到非常好的起始代码。
我推荐你使用 SAAJ 框架:
SOAP with Attachments API for Java (SAAJ)主要用于直接处理在任何 Web 服务 API 中发生在幕后的 SOAP 请求/响应消息。它允许开发人员直接发送和接收肥皂消息,而不是使用 JAX-WS。
请参阅下面使用 SAAJ 的 SOAP Web 服务调用的工作示例(运行它!)。它调用这个网络服务。
import javax.xml.soap.*;
public class SOAPClientSAAJ {
// SAAJ - SOAP Client Testing
public static void main(String args[]) {
/*
The example below requests from the Web Service at:
https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit
To call other WS, change the parameters below, which are:
- the SOAP Endpoint URL (that is, where the service is responding from)
- the SOAP Action
Also change the contents of the method createSoapEnvelope() in this class. It constructs
the inner part of the SOAP envelope that is actually sent.
*/
String soapEndpointUrl = "https://www.w3schools.com/xml/tempconvert.asmx";
String soapAction = "https://www.w3schools.com/xml/CelsiusToFahrenheit";
callSoapWebService(soapEndpointUrl, soapAction);
}
private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
SOAPPart soapPart = soapMessage.getSOAPPart();
String myNamespace = "myNamespace";
String myNamespaceURI = "https://www.w3schools.com/xml/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);
/*
Constructed SOAP Request Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myNamespace="https://www.w3schools.com/xml/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<myNamespace:CelsiusToFahrenheit>
<myNamespace:Celsius>100</myNamespace:Celsius>
</myNamespace:CelsiusToFahrenheit>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("CelsiusToFahrenheit", myNamespace);
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Celsius", myNamespace);
soapBodyElem1.addTextNode("100");
}
private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);
// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println();
soapConnection.close();
} catch (Exception e) {
System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
createSoapEnvelope(soapMessage);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
soapMessage.saveChanges();
/* Print the request message, just for debugging purposes */
System.out.println("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println("\n");
return soapMessage;
}
}
关于使用 JAXB 进行序列化/反序列化,很容易找到有关它的信息。你可以从这里开始:http ://www.mkyong.com/java/jaxb-hello-world-example/ 。