2 examples of implementing CXF methods

Source: Internet
Author: User

Reprinted from: http://www.blogjava.net/sai5201314vicky/articles/353078.html

Hello everyone, today I want to introduce a kind of technology of reality webservice--CXF

Because I am a rookie, so the following are some simple small examples! Hope to help the needy students! Well, the nonsense is not much to say, go straight to the point!

Development environment: myeclipse8.5 + tomcat6.0 + jdk1.6

CXF version: Apache-cxf-2.3.5.zip (here is a download package URL to everyone: http://www.jar114.com)

After extracting the Apache-cxf-2.3.5.zip, as shown in the directory: (Do not parse)

The following small examples are used in the Lib folder under the package (do not mind, all added to the project)!

Create a new Web project named HELLOWORLDCXF

Build a package: Com.cxf.dao, under the package to create a new interface, named: HelloWorld, the code is as follows:

Package Com.cxf.dao;

Import Javax.jws.WebService;

@WebService

Public Interface HelloWorld {

Public string SayHello (string username);

}

Build a package: Com.cxf.service, under the package to create a new class, named: Helloworldimpl, the code is as follows:

Package com.cxf.service;

Import Javax.jws.WebService;

Import Com.cxf.dao.HelloWorld;

@WebService

public class Helloworldimpl implements HelloWorld {

public string SayHello (string username) {

System.out.println ("Say hello is called!");

Return "Hello" + username;

}

}

Create a new test package: Com.cxf.action, under the package, build a class with the class name: Hellotest, the code is as follows:

Package com.cxf.action;

Import Org.apache.cxf.endpoint.Server;

Import Org.apache.cxf.jaxws.JaxWsServerFactoryBean;

Import Com.cxf.service.HelloWorldImpl;

public class Hellotest {

public static void Main (string[] args) {

Jaxwsserverfactorybean Factorybean = new Jaxwsserverfactorybean ();

Address of the Web service

Factorybean.setaddress ("Http://localhost:8081/hello");

Web Service Object Invocation interface

Factorybean.setserviceclass (Helloworldimpl.class);

Server server = Factorybean.create ();

Server.start ();

}

}

After executing the code, open in the browser: http://localhost:8081/hello?wsdl can be shown below to prove that the server has been published successfully!

Create a new Web project named Helloworldcxfclient

Create a new package, the name of the package is the same as the name of the server package: Com.cxf.dao, the interface and methods inside must also be the same as the server:

Package Com.cxf.dao;

Import Javax.jws.WebService;

@WebService

Public Interface Hello {

//The interface method here must be consistent with the method inside the server interface

Public string SayHello (string username);

}

Create a new test class: The code is as follows

Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

Import Com.cxf.dao.Hello;

Public class clienttest {

Public static void main (String []trgs) {

Jaxwsproxyfactorybean Factorybean = new Jaxwsproxyfactorybean ();

//Get server-side

Factorybean.setaddress ("Http://localhost:8081/hello");

//Get the server-side interface through the Client interface

Factorybean.setserviceclass (Hello. Class);

Hello hello = (hello) factorybean.create ();

System. out. println (Hello.sayhello ("harmonious DotA"));

}

}

Execute code, the console can print out: Hello Harmony dota!

Here, a complete cxf small example is done!

I do not know if you notice that the above interface and the real class have a tag: @WebService

The purpose of this tag is to indicate that the interface is a Web service

Of course, you can not do this way to achieve, directly with the manual to specify is also possible!! The following is the use of manual this way of realistic features, I hope you can find out the differences between the 2 methods!

Create a new Web project named HELLOWORLDCXF

Build a package: Com.cxf.dao, under the package to create a new interface, named: HelloWorld, the code is as follows:

Package Com.cxf.dao;

Import Javax.jws.WebService;

Public Interface HelloWorld {

Public string SayHello (string username);

}

Build a package: Com.cxf.service, under the package to create a new class, named: Helloworldimpl, the code is as follows:

Package com.cxf.service;

Import Javax.jws.WebService;

Import Com.cxf.dao.HelloWorld;

public class Helloworldimpl implements HelloWorld {

public string SayHello (string username) {

System.out.println ("Say hello is called!");

Return "Hello" + username;

}

}

Create a new test package: Com.cxf.action, under the package, build a class with the class name: Hellotest, the code is as follows:

Package com.cxf.action;

Import Org.apache.cxf.endpoint.Server;

Import Org.apache.cxf.jaxws.JaxWsServerFactoryBean;

Import Com.cxf.service.HelloWorldImpl;

public class Hellotest {

public static void Main (string[] args) {

Publishing Server

Serverfactorybean Factorybean = new Serverfactorybean ();

get the address of the Web service

Factorybean.setaddress ("Http://localhost:8080/hello");

Web Service Object Invocation interface

Factorybean.setserviceclass (Hello.class);

Web service object invocation implements the interface class

Helloimpl Hello = new Helloimpl ();

Factorybean.setservicebean (hello);

publishing a Web server

Factorybean.create ();

}

}

After executing the code, open in the browser: http://localhost:8081/hello?wsdl can be shown below to prove that the server has been published successfully!

Create a new Web project named Helloworldcxfclient

Create a new package, the name of the package is the same as the name of the server package: Com.cxf.dao, the interface and methods inside must also be the same as the server:

Package Com.cxf.dao;

Import Javax.jws.WebService;

Public Interface Hello {

//The interface method here must be consistent with the method inside the server interface

Public string SayHello (string username);

}

Create a new test class: The code is as follows

Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

Import Com.cxf.dao.Hello;

Public class clienttest {

Public static void main (String []trgs) {

Client Object

Clientproxyfactorybean Factorybean = new Clientproxyfactorybean ();

Get server-side

Factorybean.setaddress ("Http://localhost:8080/hello");

Get the server-side interface through the client interface

Factorybean.setserviceclass (Hello.class);

Hello hello;

Hello = (hello) factorybean.create ();

System. out. println (Hello.sayhello ("harmonious DotA"));

}

}

Execute code, the console can print out: Hello Harmony dota!

To this, completed the reality of the CXF 2 different ways!! Next, we will complete a small example of CXF integration with spring!

2 examples of implementing CXF methods

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.