I. Introduction of Wsdl2java
Wsdl2java is a tool that CXF provides for generating client-side code that is similar in function to Wsimport. However, the Wsdl2java tool can only generate code that accesses services based on CXF Publishing. Wsdl2java is located in the Cxf_home/bin directory, so you need to add a directory to path when using this tool. The parameters of the Wsdl2java tool differ from the Wsimport, mainly:
-d parameter, specifying the directory in which the code is generated
-p parameter, specifying the new package structure that is generated
(Wsdlurl) The WSDL address of the –http://server.port/service?wsdl,webservice, the required parameter
Second, new Java project, and release WebService
Create a new Java project, named Hellocxf, to import the CXF related jar packages.
Create a new Com.hellocxf.service package in the SRC directory and create a new people class with the People class Code:
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);}
Create a new Com.hellocxf.serviceImpl package in the SRC directory and create a new student class with the student code:
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; } }
New Com.hellocxf.servicePublice package is created in the SRC directory and the Servicepublish class for publishing Webservice.servicepublish code is:
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); } }
Run the Servicepublish class and publish the WebService service.
Iii. using the Wsdl2java tool to generate the client invoke service code
Create a new folder on the D drive Generatedcode
Open the cmd command and switch to the directory
Run Wsdl2java–d. HTTP://LOCALHOST:8080/PEOPLE?WSDL, generate code to access the service under the Generatedcode folder.
Iv. Create a new client project and invoke the WebService service
Create a new Java project in eclipse named Cxfclient
Copy the third generated. Java class file to the SRC directory as a whole.
Create a new Com.webservice.use package in the SRC directory and create a new class Serviceuse in the package. To complete the call to WebService, the code is:
Package com.webservice.use; Import Com.hellocxf.serviceimpl.people;import Com.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);
The service type that is created in the class Serviceuse code and the port and method of the call are obtained from the WSDL.
Access to external service services based on Wsdl2java