Use CXF to do a simple WebService example

Source: Internet
Author: User
Tags wsdl

Test simple CXF instances with a MAVEN build project

Server:

Pom.xml:

    <!--begin CXF Server -    <Dependency>        <groupId>Org.apache.cxf</groupId>        <Artifactid>Cxf-rt-frontend-jaxws</Artifactid>        <version>3.1.1</version>    </Dependency>    <Dependency>        <groupId>Org.apache.cxf</groupId>        <Artifactid>Cxf-rt-transports-http</Artifactid>        <version>3.1.1</version>    </Dependency>    <Dependency>        <!--If CXF is not integrated into the Web server, you must add the reference -        <groupId>Org.apache.cxf</groupId>        <Artifactid>Cxf-rt-transports-http-jetty</Artifactid>        <version>3.1.1</version>    </Dependency>    <!--End CXF Server -

Define webserver Access interface: Icxfservice

 Package com.cxf; Import Javax.jws.WebMethod; Import Javax.jws.WebParam; Import  = "Cxfservice", targetnamespace = "Http://localhost/services/testCXF") Public Interface  icxfservice {    @WebMethod    = "name") String name);}

Define the specific implementation of the interface: Cxfserviceimpl

 PackageCom.cxf.impl;ImportJavax.jws.WebService;ImportCom.cxf.ICXFService; @WebService (Endpointinterface= "Com.cxf.ICXFService", PortName= "HELLOCXF", ServiceName= "Hellocxfservice", targetnamespace= "Http://localhost/services/testCXF") Public classCxfserviceimplImplementsIcxfservice {@Override Publicstring Test1 (string name) {return"Hello" +name; }}

Testing Services:

 Packagecom.cxf;ImportOrg.apache.cxf.jaxws.JaxWsServerFactoryBean;ImportCom.cxf.impl.CXFServiceImpl; Public classCxfservicerun { Public Static voidMain (string[] args) {Jaxwsserverfactorybean factory=NewJaxwsserverfactorybean (); Factory.setserviceclass (Icxfservice.class); Factory.setaddress ("Http://localhost:8888/services/hello"); Factory.setservicebean (NewCxfserviceimpl ());    Factory.create (); }}

Visit: http://localhost:8888/services/hello?wsdl

Client:

Creating a CXF Client Maven project

To add a jar that CXF must:

<!--begin CXF Client -    <Dependency>        <groupId>Org.apache.cxf</groupId>        <Artifactid>Cxf-rt-frontend-jaxws</Artifactid>        <version>3.1.1</version>    </Dependency>    <Dependency>        <groupId>Org.apache.cxf</groupId>        <Artifactid>Cxf-rt-transports-http</Artifactid>        <version>3.1.1</version>    </Dependency>    <!--End CXF Client -

Download Apache CXF installation package This time using the apache-cxf-3.1.15 decompression, configuration environment variables can be used.

To configure environment variables:

After the configuration is complete and then the Command window executes wsdl2java-help The following message appears indicating the configuration was successful:

Execution:wsdl2java-encoding UTF-8-D C:\Users\ Fung Chi \desktop\cxfserver\src\main\java-p com.cxf.generate http://localhost : 8888/services/hello?wsdl

-D followed by the directory that generated the Java code, followed by the package name that generated the code, and finally the link address of the WSDL (or WSDL file path + file name)

Generated code catalog file:

Then paste the code generated by CXF into the client project (or you can directly generate the code into the client project of WebService in Eclipse)

Writing client tests: testclient

 Packagecom.cxf.client;Importjava.net.MalformedURLException;ImportJava.net.URL;ImportOrg.apache.cxf.jaxws.JaxWsProxyFactoryBean;ImportCom.cxf.generate.CxfService;ImportCom.cxf.generate.ICXFServiceService; Public classTestClient { Public Static voidMain (string[] args) {//Jaxws Call//The URL is not required unless the address of the service is changed//URL wsdlurl = null;//try {//wsdlurl = new URL ("http://localhost: 8888/services/hello?wsdl ");//} catch (Malformedurlexception e) {//e.printstacktrace ();//        }//Icxfserviceservice factory = new Icxfserviceservice (wsdlurl);////Icxfserviceservice factory = new Icxfserviceservice ();//Cxfservice cxfservice = Factory.getcxfserviceport ();//String name = Cxfservice.test1 ("Lisi");//System.out.println (name); //CXF CallJaxwsproxyfactorybean factory =NewJaxwsproxyfactorybean (); Factory.setserviceclass (Cxfservice.class); Factory.setaddress ("Http://localhost:8888/services/hello"); Cxfservice Cxfservice= Factory.create (Cxfservice.class); String name= Cxfservice.test1 ("Lisi");    SYSTEM.OUT.PRINTLN (name); }}

Both implementations are capable of

Run successfully:

Source: Https://files.cnblogs.com/files/guofz/FirstCXF.rar

Ref: 47082487

Use CXF to do a simple WebService example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.