【WebService】使用JDK開發WebService(二)


WebService的開發手段

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

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

WebService的組成

  1、服務器端

  2、客戶端

使用JDK開發WebService

  a、開發WebService服務端

    1、使用eclipse新建一個服務端java工程
    

    2、新建一個接口,使用@WebService(SEI和SEI的實現類)注解標注接口,使用@WebMethod(SEI中的所有方法)注解標注接口中定義的所有方法,如下:      

 1 package com.test.ws;
 2 
 3 import javax.jws.WebMethod;
 4 import javax.jws.WebService;
 5 
 6 /**
 7  * 定義SEI(WebService EndPoint Interface)終端
 8  * @author H__D
 9  * @date 2017年7月28日 上午11:35:34
10  *
11  */
12 //使用@WebService注解標注WebServiceI接口
13 @WebService
14 public interface HelloWS {
15     
16     //使用@WebMethod注解標注WebServiceI接口中的方法
17     @WebMethod
18     public String sayHello(String name);
19     
20 }

    3、編寫一個接口實現類,使用@WebService注解標注實現類,如下:

 1 package com.test.ws;
 2 
 3 import javax.jws.WebService;
 4 
 5 /**
 6  * SEI的具體實現
 7  * @author H__D
 8  * @date 2017年7月28日 上午11:37:43
 9  *
10  */
11 //使用@WebService注解標注
12 @WebService
13 public class HelloWSImpl implements HelloWS{
14 
15     @Override
16     public String sayHello(String name) {
17         System.out.println("WebService sayHello : " + name);
18         return "Hello : " + name;
19     }
20 }

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

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

    5、運行SeverTest類的main方法,使用瀏覽器進行訪問,訪問地址:http://127.0.0.1:8989/test-webservice/hellows,如下:
      控制他輸出:
      
      網頁訪問:
      

    6、使用Eclipse的Web Services Explorer,測試訪問WebService,其中要輸入wsdl地址(一般是發布的WebService的Endpoint后面加上'?wsdl'):
      
      
      

    7、查看發送消息和響應消息的具體內容,在Eclipse的Web Services Explorer模塊中的Status中,點擊Source如下:
      
      然后在請求框中查看源,同理在響應框中查看源,可以看到發送的請求消息和響應消息
      
      請求消息

1 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.test.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2   <soapenv:Body>
3     <q0:sayHello>
4       <arg0>tom</arg0>
5     </q0:sayHello>
6   </soapenv:Body>
7 </soapenv:Envelope>

      響應消息

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <ns2:sayHelloResponse xmlns:ns2="http://ws.test.com/">
      <return>Hello : tom</return>
    </ns2:sayHelloResponse>
  </S:Body>
</S:Envelope>

 

  b、開發WebService客戶端

    1、使用eclipse新建一個客服端java工程
      

    2、使用jdk的wsimort.exe工具生成客戶端代碼,執行命令:wsimport -keep url(url為wsdl文件的路徑)生成客戶端代碼,wsimort.exe工具位於jdk的bin目錄下,如下:
      
      打開命令行窗口,切換到src目錄中,執行命令:wsimport -keep http://127.0.0.1:8989/test-webservice/hellows?wsdl,如下:
      
      刷新工程,可以看到代碼已經生成,如下
      

    3、編寫調用WebService對外提供的方法
      wsimport工具幫我們生成了好幾個java類,但我們只需要關心WebServiceImplService類和WebServiceImpl接口的使用即可,如下:

 1 package com.test.ws.client;
 2 
 3 import com.test.ws.HelloWSImpl;
 4 import com.test.ws.HelloWSImplService;
 5 
 6 /**
 7  * 調用WebService的客戶端
 8  * @author H__D
 9  * @date 2017年7月28日 下午2:39:24
10  *
11  */
12 public class WSClient {
13 
14     public static void main(String[] args) {
15         //創建一個用於產生WebServiceImpl實例的工廠,WebServiceImplService類是wsimport工具生成的
16         HelloWSImplService factory = new HelloWSImplService();
17         //通過工廠生成一個WebServiceImpl實例,WebServiceImpl是wsimport工具生成的
18         HelloWSImpl helloWS = factory.getHelloWSImplPort();
19         System.out.println(helloWS.getClass());
20         
21         //調用WebService的sayHello方法
22         String result = helloWS.sayHello("Jack");
23         System.out.println(result);
24     }
25 }

    4、運行main方法,控制台輸出,可以看到已經成功調用了WebService,如下:
      


    

 


免責聲明!

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



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