webservice接口的開發和調用


 

一、WebService的開發手段

  使用Java開發WebService時可以使用以下兩種開發手段

    1、 使用JDK開發(1.6及以上版本)

    2、使用CXF框架開發(工作中)

二、使用JDK開發WebService

2.1、開發WebService服務器端

  1、定義一個interface,使用@WebService注解標注接口,使用@WebMethod注解標注接口中定義的所有方法,如下所示:

 
 1 package me.gacl.ws;
 2 
 3 import javax.jws.WebMethod;
 4 import javax.jws.WebService;
 5 
 6 /**
 7  * @author gacl
 8  * 定義SEI(WebService EndPoint Interface(終端))
 9  */
10 //使用@WebService注解標注WebServiceI接口
11 @WebService
12 public interface WebServiceI {
13 
14     //使用@WebMethod注解標注WebServiceI接口中的方法
15     @WebMethod
16     String sayHello(String name);
17     
18     @WebMethod
19     String save(String name,String pwd);
20 }
 

  2、編寫interface的實現類,使用@WebService注解標注實現類,實現接口中定義的所有方法,如下所示:

 
 1 package me.gacl.ws;
 2 
 3 import javax.jws.WebService;
 4 
 5 /**
 6  * @author gacl
 7  * SEI的具體實現
 8  */
 9 //使用@WebService注解標注WebServiceI接口的實現類WebServiceImpl
10 @WebService
11 public class WebServiceImpl implements WebServiceI {
12 
13     @Override
14     public String sayHello(String name) {
15         System.out.println("WebService sayHello "+name);
16         return "sayHello "+name;
17     }
18 
19     @Override
20     public String save(String name, String pwd) {
21         System.out.println("WebService save "+name+", "+pwd);
22         return "save Success";
23     }
24 }
 

  3、使用Endpoint(終端)類發布webservice,代碼如下:

 
 1 package me.gacl.ws.test;
 2 
 3 import javax.xml.ws.Endpoint;
 4 
 5 import me.gacl.ws.WebServiceImpl;
 6 
 7 /**
 8  * @author gacl
 9  *
10  * 發布Web Service
11  */
12 public class WebServicePublish {
13 
14     public static void main(String[] args) {
15         //定義WebService的發布地址,這個地址就是提供給外界訪問Webervice的URL地址,URL地址格式為:http://ip:端口號/xxxx
16         //String address = "http://192.168.1.100:8989/";這個WebService發布地址的寫法是合法的
17         //String address = "http://192.168.1.100:8989/Webservice";這個WebService發布地址的是合法的
18         String address = "http://192.168.1.100:8989/WS_Server/Webservice";
19         //使用Endpoint類提供的publish方法發布WebService,發布時要保證使用的端口號沒有被其他應用程序占用
20         Endpoint.publish(address , new WebServiceImpl());
21         System.out.println("發布webservice成功!");
22     }
23 }
 

  運行WebServicePublish類,就可以將編寫好的WebService發布好了,WebService的訪問URL是:http://192.168.1.100:8989/WS_Server/Webservice ,如下圖所示:

  

二、開發客戶端

客戶端調用我使用的兩種方式

第一種使用apche cxf生成代碼進行訪問

1、下載apache cxf的包,地址為:http://cxf.apache.org/download.html 如:apache-cxf-3.1.6

2、解壓apache-cxf-3.1.6到任意目錄

3、配置環境變量

os系統設置

1)、export CXF_HOME=/Users/moon/Desktop/tools/apache-cxf-3.1.6

2)、path后面加 :$CXF_HOME/bin

windows系統設置

1)、CXF_HOME=D:\apache-cxf-3.1.6

2)、在path后面加上 %CXF_HOME%/bin;

在命令中輸入wsdl2java,如果有提示usage,就表明配置成功

4、運行wsdl2java工具

在命令中輸入:wsdl2java -d \xx\xxx\xx -client http://localhost:8080/cxfWSServer/webservice/Greeting?wsdl

(\xx\xxx\xx 是客戶端程序代碼所在的目錄,http://localhost:8080/cxfWSServer/webservice/Greeting?wsdl 是發布的webservice服務)

附wsdl2java用法:

wsdl2java -p com -d D:\\src -all xx.wsdl

-p 指定其wsdl的命名空間,也就是要生成代碼的包名:

-d 指定要產生代碼所在目錄

-client 生成客戶端測試web service的代碼

-server 生成服務器啟動web service的代碼

-impl 生成web service的實現代碼

-ant 生成build.xml文件

-all 生成所有開始端點代碼:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file. 

生成后的代碼直接放到client工程上面

另外新建一個client類 直接使用生成的類調用

復制代碼
package com.moon.cxf;

import com.moon.cxf.client.Greeting;
import com.moon.cxf.client.GreetingImplService;

public class CxfClient {
    public static void main(String[] args) {
        
        GreetingImplService serviceFactory = new GreetingImplService();  
        Greeting service =   
            serviceFactory.getGreetingImplPort();  
          
        String result = service.greeting("Jaune");  
        System.out.println(result);  

    }
}
復制代碼

 

二、使用axis調用webservice接口

引入axis 相關jar包

 

代碼如下

復制代碼
package com.moon.cxf;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
/**
 * 使用axis調用cxf發布的webservice接口
 * @author moon
 *
 */
public class AxisClient {
    public static void main(String[] args) throws ServiceException, RemoteException {
         try {  
              
             String endpoint = " http://localhost:8080/cfxWSServer/webservice/Greeting";  
             // 調用過程  
             Service service = new Service();  

             Call call = (Call) service.createCall();  

             call.setTargetEndpointAddress(new java.net.URL(endpoint));  

             call.setOperationName(new QName("http://server.cxfWebservice.moon.com/","greeting"));// WSDL里面描述的操作名稱  

             call.addParameter("username",  
                             org.apache.axis.encoding.XMLType.XSD_STRING,  
                             javax.xml.rpc.ParameterMode.IN);// 操作的參數  

             call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 設置返回類型  

             call.setUseSOAPAction(true);  

             // 給方法傳遞參數,並且調用方法  
             String temp = "good";  
             Object[] obj = new Object[] { temp };  
             String result = (String) call.invoke(obj);  

             System.out.println("Result is : " + result);  
     } catch (Exception e) {  
             e.printStackTrace();  
     }  
    }

}
復制代碼

 

相關代碼:https://github.com/15210448683/WebServiceDemoImpl


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM