"Learning notes for the Web Service Li Gang Video"
One. Using CXF to develop Web servive server-side (new Java project name Ws_server)//Each Web service component requires 2 parts, interfaces, and implementation classes.
- Develop aWEB Service Business Interface, the interface to be decorated with @webservice, the new com.hnu.cxf.ws under the SRC package, under the package to create a new business interface called HelloWorld, HelloWorld code as follows:
1 Package com.hnu.cxf.ws; 2 3 Import Javax.jws.WebService; 4 5 @WebService// 6public interface provided by Javax.jws.WebService HelloWorld {7 string Sayhi (string name); 8 }
- Develop a Web service business interface implementation class, which is also decorated with @webservice, but to specify two properties Endpointinterface and ServiceName, Create a new Com.hnu.cxf.ws.impl package under SRC, create a new implementation class for the HelloWorld business interface, and the Helloworldws.java code is as follows:
1 PackageCom.hnu.cxf.ws.impl;2 3 Importjava.util.Date;4 5 ImportJavax.jws.WebService;6 7 ImportCom.hnu.cxf.ws.HelloWorld;8 9 //The value of Endpointinterface is the name of the interface to be implemented, and the name of the ServiceName property can be specified arbitrarilyTen@WebService (endpointinterface= "Com.hnu.cxf.ws.HelloWorld", servicename= "Helloworldws") One Public classHelloworldwsImplementsHelloWorld { A - @Override - Publicstring Sayhi (string name) { the //TODO auto-generated Method Stub - - returnName+ ", Hello! Time is now" +NewDate (); - } +}
- Import the relevant jar package for CXF and publish the service. You can also import all the jar packages in the cxf Lib directory, cxf the jar package contains the jetty Web server, and then create a new Chen package, write a main program Servermain.java, the code is as follows:
1 PackageChen;2 3 ImportJavax.xml.ws.Endpoint;4 5 ImportCom.hnu.cxf.ws.HelloWorld;6 ImportCom.hnu.cxf.ws.impl.HelloWorldWs;7 8 Public classServermain {9 Public Static voidMain (string[] args) {TenHelloWorld HW =NewHelloworldws (); One //Call Endpoint's Publish method to publish the Web service, the first parameter is the Web Service service address, AEndpoint.publish ("Http://192.168.1.102:8081/hnu", HW); -SYSTEM.OUT.PRINTLN ("Web Service exposed successfully"); - } the -}
At this point, the browser input http://192.168.1.102:8081/hnu?wsdl can appear stating that the exposure was successful.
Two. Develop Web servive client with CXF (new Java project name Ws_client)
- Call the Wsdl2java tool provided by CXF to generate the appropriate Java code based on the WSDL document. Any language implements a Web Service that needs to be provided and exposes a WSDL document.
- Find the class generated by Wsdl2java, a class that inherits (extends) the service, and an instance of that class can be used as a factory.
- Call the Getxxxport () method of an instance of the service subclass to return the Remote Web service proxy
Using CXF and spring to develop Web Service under MyEclipse