1.CXF Introduction
Apache CXF is an open-source service framework that can be used to simplify service development for users, and applications based on CXF can provide services such as soap, Xml/http, RESTFUL HTTP, or CORBA. CXF the underlying pages can use different transport protocols, including HTTP, JMS, or JBI.
According to CXF's official note, CXF contains the following features:
- Supports a large number of Web service standards, including SOAP, WS-I Basic profile, WSDL, ws-addressing, Ws-policy, ws-reliablemessaging, and ws-security.
- CXF supports a large number of front-end (frontend) programming models. CXF implements the standard Jax-ws API, which also includes a model known as simple frontend, which does not require annotation support. CXF supports two development modes for Web service: The ① rule (contract)-first development pattern, which is to develop a web Service;② code-first development pattern by writing WSDL, which is to develop webservice by writing Java code.
Having said so much, here's a look at how to use CXF.
2.CXF Download and installationDownload and install the CXF steps as follows:(1) Login CXF official site: http://cxf.apache.org/, download CXF the latest version. The author downloads the version 3.0.1. (2) Extract the downloaded compressed package to get the apache-cxf-3.0.1 folder, enter the folder can see the folder contains the directory structure as shown below. Bin: This directory holds some of the gadgets provided by CXF, the main purpose of which is to accomplish code generation tasks such as generating Java code from WSDL code and generating JavaScript code from WSDL code. docs: There is an API subdirectory under this directory that holds the API documentation for CXF. etc: This directory mainly contains some miscellaneous of the CXF framework. Lib: This directory holds the core class library of CXF and the third-party class library on which it is compiled and run. licenses: This directory holds authorization files for CXF and third-party frameworks. modules: This directory holds the jar packages CXF packaged by module. samples: This directory contains a large number of sample applications for CXF. These applications are excellent materials for learning cxf. documents such as license and readme (3) Add the bin directory under the decompression path to the system's path environment variable, so that the operating system can find the command in the Bin directory to facilitate later use of the gadget provided by CXF. (4) in order to use the CXF framework in the project, the jar package under the Lib folder needs to be added to the project. 3. Developing a Web service using CXFas a beginner, write a simple Hello world first. (1) Create a new Java project under Eclipse Cxftest(2) Add the jar package from the Bin folder in the CXF directory to the project (3) in the SRC directory, create a new package, named Test, and then create a new interface Helloworld.java, the source code is:
Package Test;import javax.jws.WebService; @WebServicepublic interface Helloworld{public string SayHello (string name);}
(4) Create the implementation class Helloworldimpl for the HelloWorld interface in the test package, with the code:
Package Test;import Javax.jws.WebService; @WebServicepublic class Helloworldimpl implements helloworld{@ Overridepublic string SayHello (string name) {System.out.println ("SayHello method called"); return ("Hello" +name);}}
(5) Create the main class Mainserver, which is used to publish WebService
Package Test;import Javax.xml.ws.endpoint;import Org.apache.cxf.endpoint.server;import Org.apache.cxf.jaxws.jaxwsserverfactorybean;public class Mainserver{<span style= "White-space:pre" ></span >public static void Main (string[] args) <span style= "White-space:pre" ></span>{//<span style= " White-space:pre "></span>//First release method: Publish Webservice//<span style= through Jaxwsserverfactorybean provided by CXF" White-space:pre "></span>jaxwsserverfactorybean factory = new Jaxwsserverfactorybean ();//<span style=" White-space:pre "></span>factory.setserviceclass (helloworldimpl.class);//<span style=" White-space: Pre "></span>factory.setaddress" ("Http://localhost:8080/HelloWorld");//<span style= "White-space:pre" ></span>//<span style= "White-space:pre" ></span>server Server = Factory.create ();//<span Style= "White-space:pre" ></span>server.start () <span style= "White-space:pre" ></span>< Span style= "White-space:pre" >≪/span>//second way, through Jax-WS provided by the endpoint to publish Webservice<span style= "White-space:pre" ></span>// First create an instance of the WebService service provider class <span style= "White-space:pre" ></span>helloworldimpl implementor = new Helloworldimpl (); <span style= "White-space:pre" ></span>string address = "http://localhost:8080/ HelloWorld "; <span style=" White-space:pre "></span>endpoint.publish (address, implementor); <span Style= "White-space:pre" ></SPAN>}}
Executing the service-side code, you can see the eclipse output as follows, indicating that WebService successfully publishedThe WSDL for the WebService service can be accessed by entering the following address http://localhost:8080/HelloWorld?wsdl in the browser.
(6) Create client code that calls WebService
Package Test;import Org.apache.cxf.jaxws.jaxwsproxyfactorybean;public class Helloworldclient{public static void Main ( String[] args) {Jaxwsproxyfactorybean factory = new Jaxwsproxyfactorybean (); Factory.setaddress ("http://localhost : 8080/helloworld "); Factory.setserviceclass (Helloworld.class); HelloWorld HelloWorld = (HelloWorld) factory.create (); System.out.println (Helloworld.sayhello ("Zhuwei"));}}
You can access the WebService service through the client.
WebService Introductory CXF Tutorial