基於jdk wsimport工具訪問外部webservice

來源:互聯網
上載者:User

標籤:jdk   wsimport   webservice   

一、Wsimport簡介

Wsimport是jdk內建的,可以根據wsdl文檔產生客戶點調用代碼的工具。無論伺服器端的webservice是用什麼語言寫的,都將在用戶端產生java代碼。伺服器端用什麼語言寫的並不重要。Wsimport位於JAVA_HOME/bin目錄下。Wsimport常用的參數有:

-d<目錄>  --將產生.class檔案。預設參數

-s<目錄>  --將產生.java檔案。

-p<產生的新包名>  --將產生類放於指定的包下

(Wsdlurl) –http://server.port/service?wsdl,webservice的wsdl地址,必須的參數

二、建立java工程,並發布webservice

建立java project,命名為HelloCXF,匯入CXF相關的jar包。

在src目錄中建立com.hellocxf.service包,並建立People類,People類代碼為:

package com.hellocxf.service; import javax.jws.WebMethod;import javax.jws.WebService; @WebServicepublic interface People {         publicString sayHello(String name);         //@WebMethod(exclude=true)         publicString makeFriend(String name);}


在src目錄中建立com.hellocxf.serviceImpl包,並建立Student類,Student代碼為:

package com.hellocxf.serviceImpl; import javax.jws.WebMethod;import javax.jws.WebService; import com.hellocxf.service.People; @WebServicepublic class Student implements People {          @Override         publicString sayHello(String name) {                   //TODO Auto-generated method stub                   System.out.println("Hello:"+name);                   returnname;         }          @WebMethod(exclude=true)         @Override         publicString makeFriend(String name) {                   //TODO Auto-generated method stub                   System.out.println("makefriend with "+name+" student");                   returnname;         } }


在src目錄建立com.hellocxf.servicePublice包,並建立ServicePublish類,用於發布webservice.ServicePublish代碼為:

package com.hellocxf.servicePublish; import javax.xml.ws.Endpoint; import com.hellocxf.service.People;import com.hellocxf.serviceImpl.Student; public class ServicePublish {          publicstatic void main(String[] args) {                   //TODO Auto-generated method stub                   PeoplemService = new Student();                                     Stringaddress = "http://localhost:8080/People";                   Endpoint.publish(address,mService);         } }


運行ServicePublish類,發布webservice服務。

三、利用wsimport工具產生用戶端調用服務代碼

在D盤建立檔案夾generatedCode

開啟cmd命令,切換到該目錄下

運行wsimport –s . http://localhost:8080/People?wsdl,在generatedCode檔案夾下產生訪問服務的代碼。

把產生的程式碼中.class檔案刪除,只保留.java的java類檔案。

四、建立用戶端工程,調用webservice服務

在eclipse中建立java工程,命名為CXFClient

將第三部產生的.java類檔案整體複製到src目錄下,複製完成之後其目錄結構如所示:


在src目錄下建立com.webservice.use包,在包中建立類ServiceUse.用於完成對webservice的調用,代碼為:

package com.webservice.use; import com.hellocxf.serviceimpl.People;importcom.hellocxf.serviceimpl.StudentService; public class ServiceUse {          publicstatic void main(String[] args) {                   //TODO Auto-generated method stub                                     //<wsdl:servicename="StudentService">                   StudentServicestudentService = new StudentService();                                     //<wsdl:portbinding="tns:StudentServiceSoapBinding"name="StudentPort">                   //<wsdl:bindingname="StudentServiceSoapBinding" type="ns1:People">                   Peoplepeople = studentService.getStudentPort();                                     StringserviceResult = people.sayHello("yinyuchun");                   System.out.println(serviceResult);                           } }


 

類ServiceUse代碼中建立的service類型和調用的連接埠、方法都是從wsdl擷取的。

基於jdk wsimport工具訪問外部webservice

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.