WEB Service technology widely used, can realize different platform data exchange, now do a CXF WebService small example, for WebService beginners reference.
1. Environment construction
1.1 Downloads Apache CXF can go to the official download http://cxf.apache.org/. You can also click Http://pan.baidu.com/s/1jIPyOYU I share the cloud disk download (apache-cxf-2.4.2). Unzip the file
1.2 Configuring the Apache CXF environment variable: Create a Cxf_hoem variable with the value cxf the root of the framework
Add a Cxf_hoem variable in path
1.3 Creating a Java project introducing the jar package in the D:\work\apache-cxf-2.4.2\lib directory
2. Code writing
2.1 Creating the Ihelloservice.java interface
Package Com.service; Import Javax.jws.WebParam; Import Javax.jws.WebService; @WebService Public Interface Ihelloservice { public string SayHello (@WebParam (name = "Text") string text); Public User GetUser (String ID);}
View Code
2.2 Creating a Helloserviceimpl.java implementation Ihelloservice interface
PackageCom.service;Importjava.util.Date;ImportJavax.jws.WebService; @WebService (Endpointinterface= "Com.service.IHelloService", ServiceName = "HelloWorld") Public classHelloserviceimplImplementsIhelloservice {@Override Publicstring SayHello (string text) {return"Hello X" +text; } @Override Publicuser getUser (String id) {User u=NewUser (); U.setid (ID); U.setage (10); U.setname ("Xiao Ming"); U.setbirthday (NewDate (NewDate (). GetTime ()-10 * 12 * 30 * 24 * 3600)); returnu; }}View Code
2.3 Writing the Webserviceapp.java class to expose Web services
package Com.service; import Javax.xml.ws.Endpoint; public class Webserviceapp { public static void main (string[] args) { SYSTEM.OUT.PRINTLN ( Web service start = new Helloserviceimpl (); String address = "Http://localhost:8080/HelloService" ; Endpoint.publish (address, implementor); SYSTEM.OUT.PRINTLN ( Web service started
View Code
2.4 The Run Webserviceapp.java class to start the service. Visit http://localhost:8080/HelloService?wsdl to see if it was successful
2.5 Writing Client Access services
======== Method 1:
Create a proxy for the client, generate a factory object, and invoke the service-side method
PackageCom.service;ImportOrg.apache.cxf.jaxws.JaxWsProxyFactoryBean; Public classhelloserviceclient { Public Static voidMain (string[] args) {Jaxwsproxyfactorybean svr=NewJaxwsproxyfactorybean (); Svr.setserviceclass (Ihelloservice.class); Svr.setaddress ("Http://localhost:8080/HelloService"); Ihelloservice HW=(Ihelloservice) svr.create (); System.out.println (Hw.sayhello (Hello)); User u= Hw.getuser ("1"); System.out.println (U.getname ()); }}
======== Method 2:
Automatic generation of client files by command
1. cmd into Doc window, enter Apache-cxf-2.4.2\bin path
2. Run Wsdl2java-frontend jaxws21-p com.client-d d:\-client-autonameresolution http://localhost:8080/HelloWorld?wsdl The Com\client folder will be generated in the D drive with the build number of the client file. Copy files to the project
3. Calling the interface
PackageCom.main;ImportCom.client.HelloWorld;ImportCom.client.IHelloService; Public classClientmain { Public Static voidMain (string[] args) {HelloWorld factory=NewHelloWorld (); Ihelloservice HW=Factory.gethelloserviceimplport (); System.out.println (Hw.sayhello ("SSS")); System.out.println (Hw.getuser ("11"). GetName ()); }}
The following links can download the above source code
Http://pan.baidu.com/s/1kUZVZHt wants to help you.
CXF WebService Complete Example