Java WebService入門實例

Web Services是由企業發佈的完成其特定商務需求的在線應用服務,其餘公司或應用軟件可以經過Internet來訪問並使用這項在線服務。java

Web Service的關鍵技術和規則:git

1.XML:描述數據的標準方法.github

2.SOAP:表示信息交換的協議(簡單對象訪問協議).web

3.WSDL:Web服務描述語言.apache

4.UDDI:通用描述、發現與集成,他是一種獨立於平臺,基於XML語言的用於在互聯網上描述商務的協議。api

1、利用JDK web服務api實現,這裏使用基於SOAP message的Web Service:瀏覽器

1.首先建立一個Web Services項目,做爲Web services Endpoint.tomcat

2.建立一個HelloService.java類 服務器

  1. package com.yjpeng.hello;    
  2.    
  3. import javax.jws.WebService;    
  4. import javax.jws.WebMethod;    
  5. import javax.xml.ws.Endpoint;    
  6. @SOAPBinding(style = SOAPBinding.Style.RPC)    
  7. @WebService    
  8. public class HelloService {    
  9.         
  10.     @WebMethod    
  11.     public String sayHello(String message){    
  12.         return "Hello ," + message;    
  13.     }    
  14.         
  15.     public static void main(String[] args) {    
  16.         //create and publish an endPoint    
  17.         HelloService hello = new HelloService();    
  18.       Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);    
  19.   }    
  20. }    

 

  1. package com.yjpeng.hello;  
  2.   
  3. import javax.jws.WebService;  
  4. import javax.jws.WebMethod;  
  5. import javax.xml.ws.Endpoint;  
  6.   
  7. @WebService  
  8. public class HelloService {  
  9.       
  10.     @WebMethod  
  11.     public String sayHello(String message){  
  12.         return "Hello ," + message;  
  13.     }  
  14.       
  15.     public static void main(String[] args) {  
  16.         //create and publish an endPoint  
  17.         HelloService hello = new HelloService();  
  18.         Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);  
  19.     }  
  20. }  

 

3.使用apt編譯HelloService.java(例如: apt -d bin(bin存放編譯後的文件目錄) scr/com/yjpeng/hello/HelloService.java)後,會生成jaxws目錄。app

4.使用java com.yjpeng.hello.HelloService執行HelloService.java文件,在瀏覽器中輸入http://localhost:8080/helloService?wsdl出現以下圖

5.使用wsimport命令生成客戶端:wsimport -p com.yjpeng.webservice -keep http://localhost:8080/helloService?wsdl 這時會在當前目錄中生成以下文件:

6.編寫好客戶端文件HelloClient.java

  1. package com.yjpeng.hello; 
  2.  
  3. import com.yjpeng.webservice.HelloServiceService; 
  4.  
  5. public class HelloClient { 
  6.     public static void main(String[] args) { 
  7.         HelloServiceService helloServiceService = new HelloServiceService(); 
  8.         com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort(); 
  9.         System.out.println(helloService.sayHello("你好")); 
  10.     } 
  11.  
  1. package com.yjpeng.hello;  
  2.   
  3. import com.yjpeng.webservice.HelloServiceService;  
  4.   
  5. public class HelloClient {  
  6.     public static void main(String[] args) {  
  7.         HelloServiceService helloServiceService = new HelloServiceService();  
  8.         com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort();  
  9.         System.out.println(helloService.sayHello("你好"));  
  10.     }  
  11.   
  12. }  

運行結果在控制檯輸出 hello,你好  證實利用JDK web服務API實現web service成功!

2、使用xfire框架,我這裏使用的是MyEclipse集成的xfire進行測試,利用xfire開發WebService能夠有三種方法:

a.一種是從JavaBean中生成。

b.一種是從wsdl文件中生成。

c.一種是本身創建webservice。

具體實現步驟以下:

1.用Myeclipse創建webService工程(注意:Web Service&J2EE Details中的Framework選XFire),目錄結構以下:

2.建立IHello.java接口

  1. package com.yjpeng.hello; 
  2.  
  3. public interface IHello { 
  4.     public String sayHello(String message); 
  1. package com.yjpeng.hello;  
  2.   
  3. public interface IHello {  
  4.     public String sayHello(String message);  
  5. }  

3.建立IHelloImpl.java實現IHello.java接口

  1. package com.yjpeng.hello; 
  2.  
  3. public class IHelloImpl implements IHello { 
  4.  
  5.     public String sayHello(String message) { 
  6.         return message; 
  7.     } 
  1. package com.yjpeng.hello;  
  2.   
  3. public class IHelloImpl implements IHello {  
  4.   
  5.     public String sayHello(String message) {  
  6.         return message;  
  7.     }  
  8. }  

4.修改Service.xml文件,加入如下代碼

<service>
<!-- 爲該Service起一個名字 -->
  <name>HelloService</name>
  <!-- service的接口類 -->
  <serviceClass>
  com.yjpeng.hello.IHello
  </serviceClass>
  <!-- service的接口實現類-->
  <implementationClass>
  com.yjpeng.hello.IHelloImpl
  </implementationClass>
  <!-- wsdl的樣式-->
  <style>wrapped</style>
  <use>literal</use>
  <scope>application</scope>
</service>

5.把該Web Service項目部署到tomcat裏面啓動tomcat在瀏覽器地址欄輸入http://localhost:8080/TestWebServices/services/Hello?wsdl 出現以下圖

6.而後在展開HelloService後面的wsdl能夠看到

7.建立一個客戶端HelloClient.java類

  1. import java.net.MalformedURLException; 
  2. import java.net.URL; 
  3.  
  4. import org.codehaus.xfire.XFireFactory; 
  5. import org.codehaus.xfire.client.Client; 
  6. import org.codehaus.xfire.client.XFireProxyFactory; 
  7. import org.codehaus.xfire.service.Service; 
  8. import org.codehaus.xfire.service.binding.ObjectServiceFactory; 
  9.  
  10. import com.yjpeng.hello.IHello; 
  11.  
  12. public class HelloClient { 
  13.     public static void main(String[] args) { 
  14.         Service s = new ObjectServiceFactory().create(IHello.class); 
  15.         XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire()); 
  16.         String url="http://192.168.122.128:8080/TestWebServices/services/HelloService"; 
  17.         IHello hello; 
  18.         try { 
  19.             hello = (IHello)xf.create(s, url); 
  20.             System.out.println(hello.sayHello("你好")); 
  21.         } catch (MalformedURLException e) { 
  22.             e.printStackTrace(); 
  23.         } 
  24.         try { 
  25.             //這個是在java端調用.net寫的遠程Web Service 若是調用本機寫的只須要把URL中的地址換成本機能訪問的地址便可 
  26.             Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl")); 
  27.             Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"}); 
  28.             System.out.println(o.length); 
  29.         } catch (Exception e) { 
  30.             e.printStackTrace(); 
  31.         } 
  32.     } 
  1. import java.net.MalformedURLException;  
  2. import java.net.URL;  
  3.   
  4. import org.codehaus.xfire.XFireFactory;  
  5. import org.codehaus.xfire.client.Client;  
  6. import org.codehaus.xfire.client.XFireProxyFactory;  
  7. import org.codehaus.xfire.service.Service;  
  8. import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  9.   
  10. import com.yjpeng.hello.IHello;  
  11.   
  12. public class HelloClient {  
  13.     public static void main(String[] args) {  
  14.         Service s = new ObjectServiceFactory().create(IHello.class);  
  15.         XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire());  
  16.         String url="http://192.168.122.128:8080/TestWebServices/services/HelloService";  
  17.         IHello hello;  
  18.         try {  
  19.             hello = (IHello)xf.create(s, url);  
  20.             System.out.println(hello.sayHello("你好"));  
  21.         } catch (MalformedURLException e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.         try {  
  25.             //這個是在java端調用.net寫的遠程Web Service 若是調用本機寫的只須要把URL中的地址換成本機能訪問的地址便可  
  26.             Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));  
  27.             Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"});  
  28.             System.out.println(o.length);  
  29.         } catch (Exception e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33. }  

運行HelloClient.java類能夠輸出 你好            1.

3、使用axis1.4開發webservice方法

首先下載axis1.4包和tomcat服務器,並將解壓後的axis1.4包下面的webapps下的axis目錄複製到tomcat服務器的webapps文件夾中。axis支持三種Web Service的部署和開發,分別爲:

a.Dynamic Invocation Interface(DII)

b.Stubs 方式

c.Dynamic Proxy方式

1.編寫DII(Dynamic Invocation Interface)方式Web Service

a.編寫服務程序HelloClient.java

  1. public class HelloClient 
  2.     public String getName(String name){ 
  3.         return "hello," + name; 
  4.     } 
  1. public class HelloClient  
  2. {  
  3.     public String getName(String name){  
  4.         return "hello," + name;  
  5.     }  
  6. }  

b.將HelloClient.java文件拷貝到axis_home下,重命名爲HelloClient.jws.

c.訪問連接http://localhost:8080/axis/HelloClient.jws?wsdl頁面顯示axis自動生成的wsdl文件

d.編寫訪問服務的客戶端TestHelloClient.java須要導入相應的axis.jar包,在下載的axis的WEB-INF/lib/目錄下。

  1. package com.yjpeng.webservice; 
  2.  
  3. import java.net.URL; 
  4. import javax.xml.namespace.QName; 
  5. import org.apache.axis.client.Call; 
  6. import org.apache.axis.client.Service; 
  7.  
  8. public class TestHelloClient { 
  9.     public static void main(String[] args){ 
  10.         try{ 
  11.             String endpoint = "http://localhost:8080/axis/HelloClient.jws"; 
  12.             Service service = new Service(); 
  13.             Call call = (Call)service.createCall(); 
  14.             call.setOperationName(new QName(endpoint, "getName")); 
  15.             call.setTargetEndpointAddress(new URL(endpoint)); 
  16.             String result = (String) call.invoke(new Object[]{"張三"}); 
  17.             System.out.println(result); 
  18.         }catch (Exception e) { 
  19.             e.printStackTrace(); 
  20.         } 
  21.     } 
  1. package com.yjpeng.webservice;  
  2.   
  3. import java.net.URL;  
  4. import javax.xml.namespace.QName;  
  5. import org.apache.axis.client.Call;  
  6. import org.apache.axis.client.Service;  
  7.   
  8. public class TestHelloClient {  
  9.     public static void main(String[] args){  
  10.         try{  
  11.             String endpoint = "http://localhost:8080/axis/HelloClient.jws";  
  12.             Service service = new Service();  
  13.             Call call = (Call)service.createCall();  
  14.             call.setOperationName(new QName(endpoint, "getName"));  
  15.             call.setTargetEndpointAddress(new URL(endpoint));  
  16.             String result = (String) call.invoke(new Object[]{"張三"});  
  17.             System.out.println(result);  
  18.         }catch (Exception e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22. }  

運行TestHelloClient.java在控制檯輸出hell,張三,測試成功.

2.編寫Dynamci Proxy方式訪問服務

a.編寫部署服務端程序,用上邊DII方式使用的HelloClient.java

  1. public class HelloClient 
  2.     public String getName(String name){ 
  3.         return "hello," + name; 
  4.     } 
  1. public class HelloClient  
  2. {  
  3.     public String getName(String name){  
  4.         return "hello," + name;  
  5.     }  
  6. }  

b.編寫代理接口HelloClientInterface.java須要擴展java.rmi.Remote類

  1. package com.yjpeng.dynamic.proxy; 
  2.  
  3. import java.rmi.Remote; 
  4. import java.rmi.RemoteException; 
  5.  
  6. public interface HelloClientInterface extends Remote { 
  7.     public String getName(String name) throws RemoteException; 
  1. package com.yjpeng.dynamic.proxy;  
  2.   
  3. import java.rmi.Remote;  
  4. import java.rmi.RemoteException;  
  5.   
  6. public interface HelloClientInterface extends Remote {  
  7.     public String getName(String name) throws RemoteException;  
  8. }  

c.編寫訪問服務的客戶端TestHelloClient.java

  1. package com.yjpeng.dynamic.proxy; 
  2.  
  3. import java.net.URL; 
  4.  
  5. import javax.xml.namespace.QName; 
  6. import javax.xml.rpc.Service; 
  7. import javax.xml.rpc.ServiceFactory; 
  8.  
  9. public class TestHelloClient { 
  10.     public static void main(String[] args){ 
  11.         try{ 
  12.             String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl"; 
  13.             String nameSpaceUrl = "http://localhost:8080/axis/HelloClient.jws"; 
  14.             String serviceName = "HelloClientService"; 
  15.             String portName = "HelloClient"; 
  16.             ServiceFactory serviceFactory = ServiceFactory.newInstance(); 
  17.             Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName)); 
  18.             HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName), 
  19.                     HelloClientInterface.class); 
  20.             System.out.println(proxy.getName("張三")); 
  21.         }catch (Exception e) { 
  22.             e.printStackTrace(); 
  23.         } 
  24.     } 
  1. package com.yjpeng.dynamic.proxy;  
  2.   
  3. import java.net.URL;  
  4.   
  5. import javax.xml.namespace.QName;  
  6. import javax.xml.rpc.Service;  
  7. import javax.xml.rpc.ServiceFactory;  
  8.   
  9. public class TestHelloClient {  
  10.     public static void main(String[] args){  
  11.         try{  
  12.             String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl";  
  13.             String nameSpaceUrl = "http://localhost:8080/axis/HelloClient.jws";  
  14.             String serviceName = "HelloClientService";  
  15.             String portName = "HelloClient";  
  16.             ServiceFactory serviceFactory = ServiceFactory.newInstance();  
  17.             Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName));  
  18.             HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName),  
  19.                     HelloClientInterface.class);  
  20.             System.out.println(proxy.getName("張三"));  
  21.         }catch (Exception e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25. }  

運行TestHelloClient.java在控制檯輸出hell,張三,測試成功.

4、使用axis2開發webservice

研究中....

5、在java web項目中開放一個webservice接口實例

1.引入須要的jar包 

2.建立一個接口類IAddNumbers.java

  1. package com.yjpeng.webservice; 
  2.  
  3. public interface IAddNumbers { 
  4.     public int addNumbers(int a, int b); 
  1. package com.yjpeng.webservice;  
  2.   
  3. public interface IAddNumbers {  
  4.     public int addNumbers(int a, int b);  
  5. }  

3.建立一個AddNumberImpl.java實現IAddnumbers.java接口類

  1. package com.yjpeng.webservice; 
  2.  
  3. import javax.jws.WebService; 
  4.  
  5. import com.sun.xml.ws.transport.http.servlet.WSServlet; 
  6.  
  7. @WebService(targetNamespace="http://webservice.yjpeng.com", serviceName="AddNumberImplService", 
  8.         portName="AddNumberImpl") 
  9. public class AddNumberImpl implements IAddNumbers { 
  10.     public int addNumbers(int a, int b) { 
  11.         return a + b; 
  12.     } 
  1. package com.yjpeng.webservice;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. import com.sun.xml.ws.transport.http.servlet.WSServlet;  
  6.   
  7. @WebService(targetNamespace="http://webservice.yjpeng.com", serviceName="AddNumberImplService",  
  8.         portName="AddNumberImpl")  
  9. public class AddNumberImpl implements IAddNumbers {  
  10.     public int addNumbers(int a, int b) {  
  11.         return a + b;  
  12.     }  
  13. }  

4.在WEN-INF目錄下建立一個sun-jaxws.xml文件

  1. <?xml version="1.0"?> 
  2. <endpoints version="2.0" 
  3.   xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"> 
  4.   <endpoint name="AddNumberImpl"  
  5.      implementation="com.yjpeng.webservice.AddNumberImpl" 
  6.      url-pattern="/addNumberImpl"/> 
  7. </endpoints> 
  8.        
  1. <?xml version="1.0"?>  
  2. <endpoints version="2.0"  
  3.   xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">  
  4.   <endpoint name="AddNumberImpl"   
  5.      implementation="com.yjpeng.webservice.AddNumberImpl"  
  6.      url-pattern="/addNumberImpl"/>  
  7.  </endpoints>  
  8.         

5.在web.xml文件中增長

  1. <servlet> 
  2.     <servlet-name>AddNumberService</servlet-name> 
  3.     <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> 
  4.     <load-on-startup>1</load-on-startup> 
  5. </servlet> 
  6. <servlet-mapping> 
  7.     <servlet-name>AddNumberService</servlet-name> 
  8.     <url-pattern>/addNumberImpl</url-pattern> 
  9. </servlet-mapping> 
  10. <listener> 
  11.     <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> 
  12. </listener> 
  1. <servlet>  
  2.     <servlet-name>AddNumberService</servlet-name>  
  3.     <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>  
  4.     <load-on-startup>1</load-on-startup>  
  5. </servlet>  
  6. <servlet-mapping>  
  7.     <servlet-name>AddNumberService</servlet-name>  
  8.     <url-pattern>/addNumberImpl</url-pattern>  
  9. </servlet-mapping>  
  10. <listener>  
  11.     <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>  
  12. </listener>  

6.部署web項目 啓動相應部署服務器。訪問http://localhost:8080/TestJaxWebService/addNumberImpl?wsdl 這樣每次啓動web服務器就開放了一個web service的接口。

相關:

cxf:源碼:https://github.com/apache/cxf

axis1 和 axis2 的簡單使用

利用AXIS開發Webservice(一) —— 如何發佈本身的webservice

Web Services是由企業發佈的完成其特定商務需求的在線應用服務,其餘公司或應用軟件可以經過Internet來訪問並使用這項在線服務。

Web Service的關鍵技術和規則:

1.XML:描述數據的標準方法.

2.SOAP:表示信息交換的協議(簡單對象訪問協議).

3.WSDL:Web服務描述語言.

4.UDDI:通用描述、發現與集成,他是一種獨立於平臺,基於XML語言的用於在互聯網上描述商務的協議。

1、利用JDK web服務api實現,這裏使用基於SOAP message的Web Service:

1.首先建立一個Web Services項目,做爲Web services Endpoint.

2.建立一個HelloService.java類 

  1. package com.yjpeng.hello;    
  2.    
  3. import javax.jws.WebService;    
  4. import javax.jws.WebMethod;    
  5. import javax.xml.ws.Endpoint;    
  6.     
  7. @WebService    
  8. public class HelloService {    
  9.         
  10.     @WebMethod    
  11.     public String sayHello(String message){    
  12.         return "Hello ," + message;    
  13.     }    
  14.         
  15.     public static void main(String[] args) {    
  16.         //create and publish an endPoint    
  17.         HelloService hello = new HelloService();    
  18.       Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);    
  19.   }    
  20. }    

 

  1. package com.yjpeng.hello;  
  2.   
  3. import javax.jws.WebService;  
  4. import javax.jws.WebMethod;  
  5. import javax.xml.ws.Endpoint;  
  6.   
  7. @WebService  
  8. public class HelloService {  
  9.       
  10.     @WebMethod  
  11.     public String sayHello(String message){  
  12.         return "Hello ," + message;  
  13.     }  
  14.       
  15.     public static void main(String[] args) {  
  16.         //create and publish an endPoint  
  17.         HelloService hello = new HelloService();  
  18.         Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);  
  19.     }  
  20. }  

 

3.使用apt編譯HelloService.java(例如: apt -d bin(bin存放編譯後的文件目錄) scr/com/yjpeng/hello/HelloService.java)後,會生成jaxws目錄。

4.使用java com.yjpeng.hello.HelloService執行HelloService.java文件,在瀏覽器中輸入http://localhost:8080/helloService?wsdl出現以下圖

5.使用wsimport命令生成客戶端:wsimport -p com.yjpeng.webservice -keep http://localhost:8080/helloService?wsdl 這時會在當前目錄中生成以下文件:

6.編寫好客戶端文件HelloClient.java

  1. package com.yjpeng.hello; 
  2.  
  3. import com.yjpeng.webservice.HelloServiceService; 
  4.  
  5. public class HelloClient { 
  6.     public static void main(String[] args) { 
  7.         HelloServiceService helloServiceService = new HelloServiceService(); 
  8.         com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort(); 
  9.         System.out.println(helloService.sayHello("你好")); 
  10.     } 
  11.  
  1. package com.yjpeng.hello;  
  2.   
  3. import com.yjpeng.webservice.HelloServiceService;  
  4.   
  5. public class HelloClient {  
  6.     public static void main(String[] args) {  
  7.         HelloServiceService helloServiceService = new HelloServiceService();  
  8.         com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort();  
  9.         System.out.println(helloService.sayHello("你好"));  
  10.     }  
  11.   
  12. }  

運行結果在控制檯輸出 hello,你好  證實利用JDK web服務API實現web service成功!

2、使用xfire框架,我這裏使用的是MyEclipse集成的xfire進行測試,利用xfire開發WebService能夠有三種方法:

a.一種是從JavaBean中生成。

b.一種是從wsdl文件中生成。

c.一種是本身創建webservice。

具體實現步驟以下:

1.用Myeclipse創建webService工程(注意:Web Service&J2EE Details中的Framework選XFire),目錄結構以下:

2.建立IHello.java接口

  1. package com.yjpeng.hello; 
  2.  
  3. public interface IHello { 
  4.     public String sayHello(String message); 
  1. package com.yjpeng.hello;  
  2.   
  3. public interface IHello {  
  4.     public String sayHello(String message);  
  5. }  

3.建立IHelloImpl.java實現IHello.java接口

  1. package com.yjpeng.hello; 
  2.  
  3. public class IHelloImpl implements IHello { 
  4.  
  5.     public String sayHello(String message) { 
  6.         return message; 
  7.     } 
  1. package com.yjpeng.hello;  
  2.   
  3. public class IHelloImpl implements IHello {  
  4.   
  5.     public String sayHello(String message) {  
  6.         return message;  
  7.     }  
  8. }  

4.修改Service.xml文件,加入如下代碼

<service>
<!-- 爲該Service起一個名字 -->
  <name>HelloService</name>
  <!-- service的接口類 -->
  <serviceClass>
  com.yjpeng.hello.IHello
  </serviceClass>
  <!-- service的接口實現類-->
  <implementationClass>
  com.yjpeng.hello.IHelloImpl
  </implementationClass>
  <!-- wsdl的樣式-->
  <style>wrapped</style>
  <use>literal</use>
  <scope>application</scope>
</service>

5.把該Web Service項目部署到tomcat裏面啓動tomcat在瀏覽器地址欄輸入http://localhost:8080/TestWebServices/services/Hello?wsdl 出現以下圖

6.而後在展開HelloService後面的wsdl能夠看到

7.建立一個客戶端HelloClient.java類

  1. import java.net.MalformedURLException; 
  2. import java.net.URL; 
  3.  
  4. import org.codehaus.xfire.XFireFactory; 
  5. import org.codehaus.xfire.client.Client; 
  6. import org.codehaus.xfire.client.XFireProxyFactory; 
  7. import org.codehaus.xfire.service.Service; 
  8. import org.codehaus.xfire.service.binding.ObjectServiceFactory; 
  9.  
  10. import com.yjpeng.hello.IHello; 
  11.  
  12. public class HelloClient { 
  13.     public static void main(String[] args) { 
  14.         Service s = new ObjectServiceFactory().create(IHello.class); 
  15.         XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire()); 
  16.         String url="http://192.168.122.128:8080/TestWebServices/services/HelloService"; 
  17.         IHello hello; 
  18.         try { 
  19.             hello = (IHello)xf.create(s, url); 
  20.             System.out.println(hello.sayHello("你好")); 
  21.         } catch (MalformedURLException e) { 
  22.             e.printStackTrace(); 
  23.         } 
  24.         try { 
  25.             //這個是在java端調用.net寫的遠程Web Service 若是調用本機寫的只須要把URL中的地址換成本機能訪問的地址便可 
  26.             Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl")); 
  27.             Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"}); 
  28.             System.out.println(o.length); 
  29.         } catch (Exception e) { 
  30.             e.printStackTrace(); 
  31.         } 
  32.     } 
  1. import java.net.MalformedURLException;  
  2. import java.net.URL;  
  3.   
  4. import org.codehaus.xfire.XFireFactory;  
  5. import org.codehaus.xfire.client.Client;  
  6. import org.codehaus.xfire.client.XFireProxyFactory;  
  7. import org.codehaus.xfire.service.Service;  
  8. import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  9.   
  10. import com.yjpeng.hello.IHello;  
  11.   
  12. public class HelloClient {  
  13.     public static void main(String[] args) {  
  14.         Service s = new ObjectServiceFactory().create(IHello.class);  
  15.         XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire());  
  16.         String url="http://192.168.122.128:8080/TestWebServices/services/HelloService";  
  17.         IHello hello;  
  18.         try {  
  19.             hello = (IHello)xf.create(s, url);  
  20.             System.out.println(hello.sayHello("你好"));  
  21.         } catch (MalformedURLException e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.         try {  
  25.             //這個是在java端調用.net寫的遠程Web Service 若是調用本機寫的只須要把URL中的地址換成本機能訪問的地址便可  
  26.             Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));  
  27.             Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"});  
  28.             System.out.println(o.length);  
  29.         } catch (Exception e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33. }  

運行HelloClient.java類能夠輸出 你好            1.

3、使用axis1.4開發webservice方法

首先下載axis1.4包和tomcat服務器,並將解壓後的axis1.4包下面的webapps下的axis目錄複製到tomcat服務器的webapps文件夾中。axis支持三種Web Service的部署和開發,分別爲:

a.Dynamic Invocation Interface(DII)

b.Stubs 方式

c.Dynamic Proxy方式

1.編寫DII(Dynamic Invocation Interface)方式Web Service

a.編寫服務程序HelloClient.java

  1. public class HelloClient 
  2.     public String getName(String name){ 
  3.         return "hello," + name; 
  4.     } 
  1. public class HelloClient  
  2. {  
  3.     public String getName(String name){  
  4.         return "hello," + name;  
  5.     }  
  6. }  

b.將HelloClient.java文件拷貝到axis_home下,重命名爲HelloClient.jws.

c.訪問連接http://localhost:8080/axis/HelloClient.jws?wsdl頁面顯示axis自動生成的wsdl文件

d.編寫訪問服務的客戶端TestHelloClient.java須要導入相應的axis.jar包,在下載的axis的WEB-INF/lib/目錄下。

  1. package com.yjpeng.webservice; 
  2.  
  3. import java.net.URL; 
  4. import javax.xml.namespace.QName; 
  5. import org.apache.axis.client.Call; 
  6. import org.apache.axis.client.Service; 
  7.  
  8. public class TestHelloClient { 
  9.     public static void main(String[] args){ 
  10.         try{ 
  11.             String endpoint = "http://localhost:8080/axis/HelloClient.jws"; 
  12.             Service service = new Service(); 
  13.             Call call = (Call)service.createCall(); 
  14.             call.setOperationName(new QName(endpoint, "getName")); 
  15.             call.setTargetEndpointAddress(new URL(endpoint)); 
  16.             String result = (String) call.invoke(new Object[]{"張三"}); 
  17.             System.out.println(result); 
  18.         }catch (Exception e) { 
  19.             e.printStackTrace(); 
  20.         } 
  21.     } 
  1. package com.yjpeng.webservice;  
  2.   
  3. import java.net.URL;  
  4. import javax.xml.namespace.QName;  
  5. import org.apache.axis.client.Call;  
  6. import org.apache.axis.client.Service;  
  7.   
  8. public class TestHelloClient {  
  9.     public static void main(String[] args){  
  10.         try{  
  11.             String endpoint = "http://localhost:8080/axis/HelloClient.jws";  
  12.             Service service = new Service();  
  13.             Call call = (Call)service.createCall();  
  14.             call.setOperationName(new QName(endpoint, "getName"));  
  15.             call.setTargetEndpointAddress(new URL(endpoint));  
  16.             String result = (String) call.invoke(new Object[]{"張三"});  
  17.             System.out.println(result);  
  18.         }catch (Exception e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22. }  

運行TestHelloClient.java在控制檯輸出hell,張三,測試成功.

2.編寫Dynamci Proxy方式訪問服務

a.編寫部署服務端程序,用上邊DII方式使用的HelloClient.java

  1. public class HelloClient 
  2.     public String getName(String name){ 
  3.         return "hello," + name; 
  4.     } 
  1. public class HelloClient  
  2. {  
  3.     public String getName(String name){  
  4.         return "hello," + name;  
  5.     }  
  6. }  

b.編寫代理接口HelloClientInterface.java須要擴展java.rmi.Remote類

  1. package com.yjpeng.dynamic.proxy; 
  2.  
  3. import java.rmi.Remote; 
  4. import java.rmi.RemoteException; 
  5.  
  6. public interface HelloClientInterface extends Remote { 
  7.     public String getName(String name) throws RemoteException; 
  1. package com.yjpeng.dynamic.proxy;  
  2.   
  3. import java.rmi.Remote;  
  4. import java.rmi.RemoteException;  
  5.   
  6. public interface HelloClientInterface extends Remote {  
  7.     public String getName(String name) throws RemoteException;  
  8. }  

c.編寫訪問服務的客戶端TestHelloClient.java

  1. package com.yjpeng.dynamic.proxy; 
  2.  
  3. import java.net.URL; 
  4.  
  5. import javax.xml.namespace.QName; 
  6. import javax.xml.rpc.Service; 
  7. import javax.xml.rpc.ServiceFactory; 
  8.  
  9. public class TestHelloClient { 
  10.     public static void main(String[] args){ 
  11.         try{ 
  12.             String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl"; 
  13.             String nameSpaceUrl = "http://localhost:8080/axis/HelloClient.jws"; 
  14.             String serviceName = "HelloClientService"; 
  15.             String portName = "HelloClient"; 
  16.             ServiceFactory serviceFactory = ServiceFactory.newInstance(); 
  17.             Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName)); 
  18.             HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName), 
  19.                     HelloClientInterface.class); 
  20.             System.out.println(proxy.getName("張三")); 
  21.         }catch (Exception e) { 
  22.             e.printStackTrace(); 
  23.         } 
  24.     } 
  1. package com.yjpeng.dynamic.proxy;  
  2.   
  3. import java.net.URL;  
  4.   
  5. import javax.xml.namespace.QName;  
  6. import javax.xml.rpc.Service;  
  7. import javax.xml.rpc.ServiceFactory;  
  8.   
  9. public class TestHelloClient {  
  10.     public static void main(String[] args){  
  11.         try{  
  12.             String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl";  
  13.             String nameSpaceUrl = "http://localhost:8080/axis/HelloClient.jws";  
  14.             String serviceName = "HelloClientService";  
  15.             String portName = "HelloClient";  
  16.             ServiceFactory serviceFactory = ServiceFactory.newInstance();  
  17.             Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName));  
  18.             HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName),  
  19.                     HelloClientInterface.class);  
  20.             System.out.println(proxy.getName("張三"));  
  21.         }catch (Exception e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25. }  

運行TestHelloClient.java在控制檯輸出hell,張三,測試成功.

4、使用axis2開發webservice

研究中....

5、在java web項目中開放一個webservice接口實例

1.引入須要的jar包 

2.建立一個接口類IAddNumbers.java

  1. package com.yjpeng.webservice; 
  2.  
  3. public interface IAddNumbers { 
  4.     public int addNumbers(int a, int b); 
  1. package com.yjpeng.webservice;  
  2.   
  3. public interface IAddNumbers {  
  4.     public int addNumbers(int a, int b);  
  5. }  

3.建立一個AddNumberImpl.java實現IAddnumbers.java接口類

  1. package com.yjpeng.webservice; 
  2.  
  3. import javax.jws.WebService; 
  4.  
  5. import com.sun.xml.ws.transport.http.servlet.WSServlet; 
  6.  
  7. @WebService(targetNamespace="http://webservice.yjpeng.com", serviceName="AddNumberImplService", 
  8.         portName="AddNumberImpl") 
  9. public class AddNumberImpl implements IAddNumbers { 
  10.     public int addNumbers(int a, int b) { 
  11.         return a + b; 
  12.     } 
  1. package com.yjpeng.webservice;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. import com.sun.xml.ws.transport.http.servlet.WSServlet;  
  6.   
  7. @WebService(targetNamespace="http://webservice.yjpeng.com", serviceName="AddNumberImplService",  
  8.         portName="AddNumberImpl")  
  9. public class AddNumberImpl implements IAddNumbers {  
  10.     public int addNumbers(int a, int b) {  
  11.         return a + b;  
  12.     }  
  13. }  

4.在WEN-INF目錄下建立一個sun-jaxws.xml文件

  1. <?xml version="1.0"?> 
  2. <endpoints version="2.0" 
  3.   xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"> 
  4.   <endpoint name="AddNumberImpl"  
  5.      implementation="com.yjpeng.webservice.AddNumberImpl" 
  6.      url-pattern="/addNumberImpl"/> 
  7. </endpoints> 
  8.        
  1. <?xml version="1.0"?>  
  2. <endpoints version="2.0"  
  3.   xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">  
  4.   <endpoint name="AddNumberImpl"   
  5.      implementation="com.yjpeng.webservice.AddNumberImpl"  
  6.      url-pattern="/addNumberImpl"/>  
  7.  </endpoints>  
  8.         

5.在web.xml文件中增長

  1. <servlet> 
  2.     <servlet-name>AddNumberService</servlet-name> 
  3.     <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> 
  4.     <load-on-startup>1</load-on-startup> 
  5. </servlet> 
  6. <servlet-mapping> 
  7.     <servlet-name>AddNumberService</servlet-name> 
  8.     <url-pattern>/addNumberImpl</url-pattern> 
  9. </servlet-mapping> 
  10. <listener> 
  11.     <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> 
  12. </listener> 
  1. <servlet>  
  2.     <servlet-name>AddNumberService</servlet-name>  
  3.     <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>  
  4.     <load-on-startup>1</load-on-startup>  
  5. </servlet>  
  6. <servlet-mapping>  
  7.     <servlet-name>AddNumberService</servlet-name>  
  8.     <url-pattern>/addNumberImpl</url-pattern>  
  9. </servlet-mapping>  
  10. <listener>  
  11.     <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>  
  12. </listener>  

6.部署web項目 啓動相應部署服務器。訪問http://localhost:8080/TestJaxWebService/addNumberImpl?wsdl 這樣每次啓動web服務器就開放了一個web service的接口。

相關:

cxf:源碼:https://github.com/apache/cxf

axis1 和 axis2 的簡單使用

利用AXIS開發Webservice(一) —— 如何發佈本身的webservice

相關文章
相關標籤/搜索