jax-ws創建和訪問webservice

目前webservice在各個行業中的應用都十分的火。市面上目前流行的關於webservice的框架也有很多,今天我們不套用外部的框架,而是通過純java來一步一步實現webservice的服務端的創建和客戶端的訪問。 

一、創建webservice的服務器端 

整體步驟如下: 
1、創建服務的接口 
2、創建服務的實現類 
3、發佈服務 
4、驗證服務是否發佈成功 

二、創建webservice的客戶端 

整體步驟如下: 
1、通過java.net.URL定義連接webservice服務端的URL地址 
2、通過javax.xml.namespace.QName設置命名空間和本地服務名稱 
3、通過javax.xml.ws.Service創建一個從客戶端到服務器端的連接 
4、通過javax.xml.ws.Service的getPort獲取接口類 
5、通過接口訪問相應的方法 


三、案例演示 

(一)、創建webservice的服務器端 

1、創建接口類 

通過java的註解@WebService標明此接口是一個WebService接口 

Java代碼 
  1. package com.sample.server;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebService;  
  5.   
  6. import com.sample.bean.Student;  
  7.   
  8. @WebService  
  9. public interface MyService {  
  10.   
  11.     @WebMethod  
  12.     public String getStuName(String id);  
  13.       
  14.     public Student getStuInfo(String id);  
  15. }  






2、創建接口的實現類 

這裏需要注意,一定要設置endpointInterface。它的值爲接口的全稱,一定要帶上包名。 

Java代碼 
  1. package com.sample.server;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.jws.WebService;  
  6.   
  7. import com.sample.bean.Student;  
  8. @WebService(endpointInterface="com.sample.server.MyService")  
  9. public class MyServiceImpl implements MyService {  
  10.   
  11.     @Override  
  12.     public String getStuName(String id) {  
  13.         if("1".equals(id)) {  
  14.             System.out.println("getStuName");  
  15.             return "張三" ;  
  16.         }  
  17.         return "";  
  18.     }  
  19.   
  20.     @Override  
  21.     public Student getStuInfo(String id) {  
  22.         Student student = new Student();  
  23.         if("1".equals(id)) {  
  24.             System.out.println("getStuInfo");  
  25.             student.setId("1");  
  26.             student.setName("張三");  
  27.             student.setPassword("123456");  
  28.             student.setAge(12);  
  29.             student.setMakedate(new Date());  
  30.         }  
  31.         return student;  
  32.     }  
  33.   
  34. }  


3、發佈服務 

可以在這裏創建一個Server類。通過Endpoint的publish方法發佈服務,代碼中的http://localhost:8082/stu爲此服務的發佈地址,new MyServiceImpl()表示服務的實現類。運行main方法即可發佈成功,如果發現有錯誤跑出,查看一下端口號是否可用。 

Java代碼 
  1. package com.sample.server;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5.   
  6. public class Server {  
  7.   
  8.     public static void main(String[] args) {  
  9.         Endpoint.publish("http://localhost:8082/stu"new MyServiceImpl());  
  10.     }  
  11. }  


4、驗證服務是否發佈成功 

在瀏覽器中輸入http://localhost:8082/stu,如果出現以下內容表示服務已經發布成功 
: 

Web Services 
No JAX-WS context information available. 


在瀏覽器中輸入http://localhost:8082/stu?wsdl就可以查看生成的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.6 in JDK 6. 
  --> 
- <!--  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. 
  --> 
- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.sample.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.sample.com/" name="MyServiceImplService"> 
- <types> 
- <xsd:schema> 
  <xsd:import namespace="http://server.sample.com/" schemaLocation="http://localhost:8082/stu?xsd=1" /> 
  </xsd:schema> 
  </types> 
- <message name="getStuName"> 
  <part name="parameters" element="tns:getStuName" /> 
  </message> 
- <message name="getStuNameResponse"> 
  <part name="parameters" element="tns:getStuNameResponse" /> 
  </message> 
- <message name="getStuInfo"> 
  <part name="parameters" element="tns:getStuInfo" /> 
  </message> 
- <message name="getStuInfoResponse"> 
  <part name="parameters" element="tns:getStuInfoResponse" /> 
  </message> 
- <portType name="MyService"> 
- <operation name="getStuName"> 
  <input message="tns:getStuName" /> 
  <output message="tns:getStuNameResponse" /> 
  </operation> 
- <operation name="getStuInfo"> 
  <input message="tns:getStuInfo" /> 
  <output message="tns:getStuInfoResponse" /> 
  </operation> 
  </portType> 
- <binding name="MyServiceImplPortBinding" type="tns:MyService"> 
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <operation name="getStuName"> 
  <soap:operation soapAction="" /> 
- <input> 
  <soap:body use="literal" /> 
  </input> 
- <output> 
  <soap:body use="literal" /> 
  </output> 
  </operation> 
- <operation name="getStuInfo"> 
  <soap:operation soapAction="" /> 
- <input> 
  <soap:body use="literal" /> 
  </input> 
- <output> 
  <soap:body use="literal" /> 
  </output> 
  </operation> 
  </binding> 
- <service name="MyServiceImplService"> 
- <port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding"> 
  <soap:address location="http://localhost:8082/stu" /> 
  </port> 
  </service> 
  </definitions> 


(二)、創建客戶端 

1、創建客戶端類MyClient 

這裏解釋一下QName的兩個參數: 
第一個參數值是上面生成的wsdl文件中targetNamespace的值,我的爲"http://server.sample.com/"。這個值實際上就是包名的反轉。 


第二個參數爲: 

name="MyServiceImplService">
 


運行此方法可以在控制檯打印出getStuName,表示訪問成功。 

Java代碼 
  1. package com.sample.client;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.net.URL;  
  5.   
  6. import javax.xml.namespace.QName;  
  7. import javax.xml.ws.Service;  
  8.   
  9. import com.sample.server.MyService;  
  10.   
  11.   
  12. public class MyClient {  
  13.   
  14.     public static void main(String[] args) throws MalformedURLException {  
  15.         String address = "http://localhost:8082/stu?wsdl";  
  16.         URL url = new URL(address);  
  17.         QName qname = new QName("http://server.sample.com/""MyServiceImplService");  
  18.         Service service = Service.create(url,qname);  
  19.         MyService tMyservice = service.getPort(MyService.class);  
  20.         tMyservice.getStuName("1");  
  21.     }  
  22. }  


(三)、遺留問題 

上面這種訪問方式,我們會發現在客戶端中要依賴MyService這個類,而實際工作中,我們根本無法獲取到MyService,服務商也不可能爲我們提供這個接口,所以,我們將在下一節中通過wsimport來解決這個問題。 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章