作者LCHENAN (~!!)
看板java
标题[问题] Web Service SOAP 之SOAPMessage解读问题
时间Mon Jun 15 16:51:12 2009
各位前辈您好
小弟在工作上碰到了一个问题,我已经爬文过,也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="
http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:sayHelloResponse xmlns:ns2="
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="
http://schemas.xmlsoap.org/soap/envelope/">
<S:Header/>
<S:Body>
<ns2:sayHello xmlns:ns2="
http://myself.idv.tw/"/>
</S:Body>
</S:Envelope>
-----------------------------------------------------------------
SOAP 回应
-----------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="
http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:sayHelloResponse xmlns:ns2="
http://myself.idv.tw/">
<return>Hello!!!!</return>
</ns2:sayHelloResponse>
</S:Body>
</S:Envelope>
//=====================================================================================
4.
以下是执行Client端时所产生的讯息,我有自行分行过了
以下是送出时的SOAPMessage
<SOAP-ENV:Envelope xmlns:SOAP-ENV="
http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns2:sayHello xmlns:ns2="
http://myself.idv.tw/">
<argo>Frederic</argo>
</ns2:sayHello>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
//以下是回应时的SOAPMessage
<?xml version="1.0" ?>
<S:Envelope xmlns:S="
http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:sayHelloResponse xmlns:ns2="
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
1F:推 ppkkykei:NetBeans官网有web services教学文件. 06/16 00:12