一、创建并发布一个简单的webservice应用
1、webservice 代码:
1 package com.ls.demo; 2 3 import javax.jws.WebMethod; 4 import javax.jws.WebService; 5 import javax.xml.ws.Endpoint; 6 7 8 @WebService 9 public class HelloWorld { 10 @WebMethod 11 public String sayHello(String str){ 12 System.out.println("get Message..."); 13 String result = "Hello World, "+str; 14 return result; 15 } 16 public static void main(String[] args) { 17 System.out.println("server is running"); 18 String address="http://localhost:9000/HelloWorld"; 19 Object implementor =new HelloWorld(); 20 Endpoint.publish(address, implementor); 21 } 22 23 }
2、运行项目,并访问 "http://localhost:9000/HelloWorld?wsdl",得到如下wsdl文件,说明webservice发布成功:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --> 3 <!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --> 4 <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://demo.ls.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://demo.ls.com/" name="HelloWorldService"> 5 <types> 6 <xsd:schema> 7 <xsd:import namespace="http://demo.ls.com/" schemaLocation="http://localhost:9000/HelloWorld?xsd=1"></xsd:import> 8 </xsd:schema> 9 </types> 10 <message name="sayHello"> 11 <part name="parameters" element="tns:sayHello"></part> 12 </message> 13 <message name="sayHelloResponse"> 14 <part name="parameters" element="tns:sayHelloResponse"></part> 15 </message> 16 <portType name="HelloWorld"> 17 <operation name="sayHello"> 18 <input wsam:Action="http://demo.ls.com/HelloWorld/sayHelloRequest" message="tns:sayHello"></input> 19 <output wsam:Action="http://demo.ls.com/HelloWorld/sayHelloResponse" message="tns:sayHelloResponse"></output> 20 </operation> 21 </portType> 22 <binding name="HelloWorldPortBinding" type="tns:HelloWorld"> 23 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding> 24 <operation name="sayHello"> 25 <soap:operation soapAction=""></soap:operation> 26 <input> 27 <soap:body use="literal"></soap:body> 28 </input> 29 <output> 30 <soap:body use="literal"></soap:body> 31 </output> 32 </operation> 33 </binding> 34 <service name="HelloWorldService"> 35 <port name="HelloWorldPort" binding="tns:HelloWorldPortBinding"> 36 <soap:address location="http://localhost:9000/HelloWorld"></soap:address> 37 </port> 38 </service> 39 </definitions>
二、客户端访问webservice
1、通过 HttpClient 及 HttpURLConnection 发送SOAP请求,代码如下:
1 import java.io.BufferedReader; 2 import java.io.DataOutputStream; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 8 import org.apache.commons.httpclient.HttpClient; 9 import org.apache.commons.httpclient.methods.PostMethod; 10 import org.apache.commons.httpclient.methods.RequestEntity; 11 import org.apache.commons.httpclient.methods.StringRequestEntity; 12 import org.apache.commons.io.IOUtils; 13 14 public class TestHelloWrold { 15 public static void main(String[] args) throws Exception { 16 String wsdl = "http://localhost:9000/HelloWorld?wsdl"; 17 int timeout = 10000; 18 StringBuffer sb = new StringBuffer(""); 19 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 20 sb.append("<soap:Envelope " 21 + "xmlns:api='http://demo.ls.com/' " 22 + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " 23 + "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " 24 + "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"); 25 sb.append("<soap:Body>"); 26 sb.append("<api:sayHello>"); 27 sb.append("<arg0>ls</arg0>"); 28 sb.append("</api:sayHello>"); 29 sb.append("</soap:Body>"); 30 sb.append("</soap:Envelope>"); 31 32 33 34 // HttpClient发送SOAP请求 35 System.out.println("HttpClient 发送SOAP请求"); 36 HttpClient client = new HttpClient(); 37 PostMethod postMethod = new PostMethod(wsdl); 38 // 设置连接超时 39 client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); 40 // 设置读取时间超时 41 client.getHttpConnectionManager().getParams().setSoTimeout(timeout); 42 // 然后把Soap请求数据添加到PostMethod中 43 RequestEntity requestEntity = new StringRequestEntity(sb.toString(), "text/xml", "UTF-8"); 44 //设置请求头部,否则可能会报 “no SOAPAction header” 的错误 45 postMethod.setRequestHeader("SOAPAction",""); 46 // 设置请求体 47 postMethod.setRequestEntity(requestEntity); 48 int status = client.executeMethod(postMethod); 49 // 打印请求状态码 50 System.out.println("status:" + status); 51 // 获取响应体输入流 52 InputStream is = postMethod.getResponseBodyAsStream(); 53 // 获取请求结果字符串 54 String result = IOUtils.toString(is); 55 System.out.println("result: " + result); 56 57 58 59 // HttpURLConnection 发送SOAP请求 60 System.out.println("HttpURLConnection 发送SOAP请求"); 61 URL url = new URL(wsdl); 62 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 63 64 conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); 65 conn.setRequestMethod("POST"); 66 conn.setUseCaches(false); 67 conn.setDoInput(true); 68 conn.setDoOutput(true); 69 conn.setConnectTimeout(timeout); 70 conn.setReadTimeout(timeout); 71 72 DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); 73 dos.write(sb.toString().getBytes("utf-8")); 74 dos.flush(); 75 76 77 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); 78 String line = null; 79 StringBuffer strBuf = new StringBuffer(); 80 while ((line = reader.readLine()) != null) { 81 strBuf.append(line); 82 } 83 dos.close(); 84 reader.close(); 85 86 System.out.println(strBuf.toString()); 87 } 88 89 }
响应报文如下:
<?xml version="1.0" ?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:sayHelloResponse xmlns:ns2="http://demo.ls.com/"> <return>Hello World, ls</return> </ns2:sayHelloResponse> </S:Body> </S:Envelope>
问:SOAP的请求报文的格式是怎么来的呢?
答:可用Eclipse测试WSDL文件,则可得到想要的SOAP请求及响应报文,具体步骤如下图:
第一步:
第二步:
通过第一步,会在浏览器打开如下的页面
2、生成客户端代码访问
a、通过 "wsimport"(JDK自带)命令生成客户端代码。进入命令行模式,执行 wsimport -s . http://localhost:9000/HelloWorld?wsdl,就会在当前目录下生成客户端代码。附图:
b、通过Eclipse生成客户端代码