在本篇技巧文章中,作者兼開發(fā)人員 Nicholas Chase 向您演示如何使用用于 xml 消息傳遞的 java API(Java API for XML Messaging (JAXM))簡化創(chuàng)建和發(fā)送 SOAP 消息的過程。 Web 服務的基礎在于以標準格式發(fā)送和接收消息以便使所有系統(tǒng)都能理解。通常,那種格式是簡單對象訪問協(xié)議(Simple Object accessPRotocol (SOAP))。SOAP 消息可以手工生成和發(fā)送,但是用于 XML 消息傳遞的 Java API(JAXM)使許多必需步驟(如創(chuàng)建連接或創(chuàng)建并發(fā)送實際消息)自動化。這篇技巧文章記錄了一個同步 SOAP 消息的創(chuàng)建和發(fā)送。
這個過程包含五個步驟:
創(chuàng)建 SOAP 連接 創(chuàng)建 SOAP 消息 填充消息 發(fā)送消息 檢索應答 JAXM 可以作為 Java XML Pack(2002 年春季版)的一部分和 Java Web Services Developer Pack EA2(請參閱參考資料)的一部分而獲得。后者還包含了一份 Tomcat Web 服務器以及樣本應用程序的副本。那些樣本 Web 服務之一作為本技巧文章中 SOAP 消息的目的地,這個例子中實際服務的內容和功能卻不是很重要。
SOAP 消息結構 一個基本的 SOAP 消息由包含兩個主要部分(報頭和主體)的封套組成。應用程序決定如何使用這些部分,但整個消息必須遵循特定的 XML 結構,例如:
//Next, create the actual message MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage message = messageFactory.createMessage();
//Create objects for the message parts SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody();
try { ... //Create objects for the message parts SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody();
//Populate the body //Create the main element and namespace SOAPElement bodyElement = body.addChildElement(envelope.createName("schedule" , "cal", "http://www.example.com/calendar")); //Add content bodyElement.addChildElement("cal:newitem").addTextNode("contentHere");
//Save the message message.saveChanges();
//Check the input System.out.println(" REQUEST: "); message.writeTo(System.out); System.out.println();
public static void main(String args[]) { ... //Create objects for the message parts SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody();
//Populate the Message StreamSource preppedMsgSrc = new StreamSource( new FileInputStream("prepped.msg")); soapPart.setContent(preppedMsgSrc);
... //Send the message SOAPMessage reply = connection.call(message, destination);
//Check the output System.out.println(" RESPONSE: "); //Create the transformer TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); //Extract the content of the reply Source sourceContent = reply.getSOAPPart().getContent(); //Set the output for the transformation StreamResult result = new StreamResult(System.out); transformer.transform(sourceContent, result); System.out.println();
//Close the connection connection.close(); ... } }
請查看 W3C 中的各種與 Web 服務相關的建議書的情況。 JAXM 可以作為 Java XML Pack(2002 年春季版)的一部分和 Java Web Services Developer Pack EA2 的一部分而獲得。 IBM WebSphere Studio application Developer 是用于構建、測試和部署 Web 服務的易用的集成開發(fā)環(huán)境。 要獲取完整的 Web 服務工具箱,請下載 IBM 的 Web Services Development Kit。 在 developerWorks 的 XML 和 Web 服務專區(qū)查找更多參考資料。
關于作者 Nicholas Chase 一直在參與如 LUCent Technologies、Sun Microsystems、Oracle 和 Tampa Bay Buccaneers 等公司的網站開發(fā)。Nick 曾經是一位高中物理教師、低級放射性廢物設備的治理員、在線科幻小說雜志的編輯、多媒體工程師和 Oracle 講師。近來,他是佛羅里達州克利爾沃特 Site Dynamics Interactive Communications 的首席技術官,而且是有關 Web 開發(fā)的三本書,包括 Java and XML From Scratch(Que)和即將出版的 Primer Plus XML Programming(Sams)的作者。他愿意傾聽讀者的意見,可以通過 [email protected] 與他聯(lián)系。