我理解你的問題歸結爲如何從Java調用SOAP(JAX-WS)Web服務並獲取其返回對象。在這種情況下,您有兩種可能的方法:
- 通過
wsimport
生成Java類並使用它們;或
- 創建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:
- 使用OXM(對象到XML映射)框架,例如JAXB序列化/反序列化從XML /成對象
- 或者,如果您必須手動創建/解析XML(如果接收的對象與發送的對象只有一點差異,這可能是最好的解決方案)。
創建使用經典java.net.HttpUrlConnection
SOAP客戶端並不難(但不是簡單其一),你可以在this link一個很好的起點代碼中找到。
我建議你使用SAAJ框架:
SOAP用於Java(SAAJ)附件API主要用於直接與發生在幕後的任何Web服務的SOAP請求/響應消息處理API。它允許開發人員直接發送和接收SOAP消息,而不是使用JAX-WS。
請參閱下面的使用SAAJ的SOAP Web服務調用的工作示例(運行它!)。它叫this web service。
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/。
你的問題有點不清楚。你想創建的方法將(1)從Web服務獲取對象; (2)與對象一起工作;和(3)將其發回Web服務。是嗎? – acdcjunior 2013-04-11 03:36:28
不,對象將在客戶端創建,它將被髮送到調用中的ws,ws將設置一個變量,例如currentTime,執行一些業務邏輯,如將其存儲在數據庫中,然後發送給將對象返回給客戶端並設置currentTime。希望我能更好地解釋我的自我。謝謝。 – jpz 2013-04-11 04:57:09