CXF tutorial for getting started With WebService

Source: Internet
Author: User

CXF tutorial for getting started With WebService

1. CXF Overview

Apache CXF is an open-source Service framework that can be used to simplify service development for users. applications developed based on CXF can provide services such as SOAP, XML/HTTP, restful http, and CORBA. The CXF underlying page can use different transmission protocols, including HTTP, JMS, and JBI.

According to the official instructions of CXF, CXF includes the following features:

Supports a wide range 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 frontend programming models. CXF implements the standard JAX-WS API, which also includes a model called simple frontend, which does not require annotation support. CXF supports two development modes of web Services: ① A contract development mode, that is, developing web services by writing WSDL; ② a code-first development mode, webservice is developed by writing java code. now let's take a look at how to use CXF.

2. Download and install CXF

To download and install CXF, follow these steps:

(1) log on to the CXF official site: http://cxf.apache.org/to download the latest cxfversion. I downloaded version 3.0.1.

(2) decompress the downloaded package to get the apache-cxf-3.0.1 folder, enter the folder, you can see that the folder contains the following directory structure.

Bin: This directory stores some gadgets provided by CXF. These tools are mainly used to generate java code based on the WSDL code and generate javascript code based on the WSDL code.

Docs: There is an api subdirectory under this Directory, which stores the cxf api documentation.

Etc: This directory stores some miscellaneous of the cxf framework.

Lib: This directory stores the core class libraries of CXF and third-party class libraries on which compilation and operation depend.

Licenses: This directory stores CXF and third-party framework authorization files.

Modules: This directory stores the jar packages packaged by modules for CXF.

Samples: This directory stores a large number of example applications of CXF. These applications are excellent materials for learning CXF.

License, readme, and other documents

(3) Add the bin directory in the decompressed path to the path environment variable of the system, so that the operating system can find the commands in the bin directory, so that you can use the gadgets provided by CXF in the future.

(4) to use the CXF framework in the project, add the jar package in the lib folder to the project.

3. Use CXF to develop web Services

As a beginner, write the simplest hello world.

(1) Create a New java project CXFTest under eclipse

(2) Add the jar package in the bin folder under the CXF decompression directory to the project.

(3) create a package under the src directory, name it test, and create an interface HelloWorld. java. The source code is:

Package test;

Import javax. jws. WebService;

@ WebService
Public interface HelloWorld
{
Public String sayHello (String name );
}

(4) Create the HelloWorld interface implementation class HelloWorldImpl in the test package. The code is:

Package test;

Import javax. jws. WebService;

@ WebService
Public class HelloWorldImpl implements HelloWorld
{

@ Override
Public String sayHello (String name)
{

System. out. println ("sayHello method called ");
Return ("Hello" + name );

}

}

(5) create a main-class MainServer for publishing webservice

Package test;


Import javax. xml. ws. Endpoint;


Import org. apache. cxf. endpoint. Server;
Import org. apache. cxf. jaxws. JaxWsServerFactoryBean;


Public class MainServer
{


Public static void main (String [] args)
{
/// Method 1: publish webservice through JaxWsServerFactoryBean provided by CXF
// JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean ();
// Factory. setServiceClass (HelloWorldImpl. class );
// Factory. setAddress ("http: // localhost: 8080/HelloWorld ");
//
// Server server = factory. create ();
// Server. start ();

// Second, publish webservice through the Endpoint provided by the JAX-WS
// First create an instance of the webservice class
HelloWorldImpl implementor = new HelloWorldImpl ();
String address = "http: // localhost: 8080/HelloWorld ";
Endpoint. publish (address, implementor );

}

}

Run the server code. The eclipse output is as follows, indicating that webservice is successfully released.

Enter the following URL in the browser: http: // localhost: 8080/HelloWorld? To access the wsdl of the webservice.

(6) Create the client code to call 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 through the client.

Basic Apache CXF quick start tutorial

Apache CXF practice

For details about Apache CXF, click here
Apache CXF: click here

This article permanently updates the link address:

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.