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

看板java作者 (~!!)時間15年前 (2009/06/15 16:51), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/3 (看更多)
各位前輩您好 小弟在工作上碰到了一個問題,我已經爬文過,也Google過了 我主要是閱讀 The Java Web Services Tutorial For Java Web Services Developer's Pack, V2.0 February 17,2006 這本手冊 現在所碰到的問題是 我想要將SOAP所回應的訊息的Body部份解讀出來 例如Client送出訊息之後,Server端回應給Client端的SOAPMessage如下 <?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> 此段訊息,我想要將Hello!!!!這個字在Client端印出來 但是 我試過了此手冊的 P.133頁的方法,都一直無法成功 請問各位前輩,有人能教我怎麼處理這段訊息,或者能給予我一些參考的資料嗎?? 我在這地方卡了三天了,請各位幫忙 感謝 執行環境 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端程式碼: 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!!!!"; } } //========================================================================================= 2. Client端程式碼: 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(); } //========================================================================================= 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> //===================================================================================== 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

06/16 00:12, , 1F
NetBeans官網有web services教學文件.
06/16 00:12, 1F
文章代碼(AID): #1ADWk1Zt (java)
文章代碼(AID): #1ADWk1Zt (java)