Re: [問題] Web Service SOAP 之SOAPMessage解讀問題

看板java作者 (~!!)時間15年前 (2009/06/16 16:12), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
如果需要使用網頁觀看者,可點選此處 http://www.javaworld.com.tw/jute/post/view?bid=19&id=260902&sty=3 以下為可編譯成功的程式碼 這隻程式是一個很簡單的Simple SOAP 程式主要的目的在於 當Server收到來自於Client端的SOAP訊息之後 會將所需要回傳的SOAP傳回去 而Client端會解讀傳回的SOAP資訊 並將需要的訊息印出來 程式碼是SOAP的Server端與Client端部份 供大家參考,如果有錯的地方,煩請大家指正!! 執行環境 Windows XP NetBeans, GlassFish V2.1, JDK 1.6.0_14 以下是我的程式碼 以下主要分成: 1. Server端程式碼 2. Client端程式碼 3. 我使用GlassFish的Web Service的Test所回應出來的訊息 4. 執行的結果 5. WSDL //========================================================================================= 1. Server端程式碼: [code] package tw.idv.myself; import javax.jws.WebMethod; import javax.jws.WebService; @WebService() public class Hello { @WebMethod(operationName = "sayHello") public String sayHello() { //TODO write your implementation code here: return "Hello!!!!"; } } [/code] //========================================================================================= 2. Client端程式碼: [code] public class Main { public static void main(String[] args) throws SOAPException, MalformedURLException, IOException { SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); SOAPHeader header = message.getSOAPHeader(); SOAPBody body = message.getSOAPBody(); QName bodyName = new QName("http://myself.idv.tw/", "sayHello", "ns2"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); URL endpoint = new URL("http://localhost:8080/MyServer/HelloService"); message.writeTo(System.out); System.out.println(""); SOAPMessage response = connection.call(message, endpoint); response.writeTo(System.out); System.out.println(""); connection.close(); SOAPBody responsebody = response.getSOAPBody(); QName responsebodyname = new QName("http://myself.idv.tw/","sayHelloResponse","ns2"); Iterator iterator = responsebody.getChildElements(responsebodyname); //這邊是Body裡面的元素名稱 QName elementname = new QName("return"); SOAPElement responsereturn = (SOAPElement)iterator.next(); Iterator lastPrice = responsereturn.getChildElements(elementname); SOAPElement responseelement = (SOAPElement)lastPrice.next(); String str = responseelement.getValue(); System.out.println(str); } [/code] //========================================================================================= 3. 我使用GlassFish的Web Service的test訊息 SOAP 請求 ----------------------------------------------------------------- <?xml version="1.0" encoding="UTF-8"?> <S:Envelope xmlns:S="" rel="nofollow">http://schemas.xmlsoap.org/soap/envelope/"> <S:Header/> <S:Body> <ns2:sayHello xmlns:ns2="" rel="nofollow">http://myself.idv.tw/"/> </S:Body> </S:Envelope> ----------------------------------------------------------------- SOAP 回應 ----------------------------------------------------------------- <?xml version="1.0" encoding="UTF-8"?> <S:Envelope xmlns:S="" rel="nofollow">http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:sayHelloResponse xmlns:ns2="" rel="nofollow">http://myself.idv.tw/"> <return>Hello!!!!</return> </ns2:sayHelloResponse> </S:Body> </S:Envelope> //===================================================================================== 4. 以下是執行Client端時所產生的訊息,我有自行分行過了 以下是送出時的SOAPMessage <SOAP-ENV:Envelope xmlns:SOAP-ENV="" rel="nofollow">http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <ns2:sayHello xmlns:ns2="" rel="nofollow">http://myself.idv.tw/"> <argo>Frederic</argo> </ns2:sayHello> </SOAP-ENV:Body> </SOAP-ENV:Envelope> //以下是回應時的SOAPMessage <?xml version="1.0" ?> <S:Envelope xmlns:S="" rel="nofollow">http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:sayHelloResponse xmlns:ns2="" rel="nofollow">http://myself.idv.tw/"> <return>Hello!!!!</return> </ns2:sayHelloResponse> </S:Body> </S:Envelope> //抓出來的return元素 Hello!!!! //===================================================================================== 5.WSDL <?xml version="1.0" encoding="UTF-8" ?> - <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-749-SNAPSHOT. --> - <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-749-SNAPSHOT. --> - <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns: soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://myself.idv.tw/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://myself.idv.tw/" name="HelloService"> - <types> - <xsd:schema> <xsd:import namespace="http://myself.idv.tw/" schemaLocation="http://localhost:8080/MyServer/HelloService?xsd=1" /> </xsd:schema> </types> - <message name="sayHello"> <part name="parameters" element="tns:sayHello" /> </message> - <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse" /> </message> - <portType name="Hello"> - <operation name="sayHello"> <input message="tns:sayHello" /> <output message="tns:sayHelloResponse" /> </operation> </portType> - <binding name="HelloPortBinding" type="tns:Hello"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <operation name="sayHello"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> </binding> - <service name="HelloService"> - <port name="HelloPort" binding="tns:HelloPortBinding"> <soap:address location="http://localhost:8080/MyServer/HelloService" /> </port> </service> </definitions> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.125.136.75
文章代碼(AID): #1ADrG7NY (java)
文章代碼(AID): #1ADrG7NY (java)