Use Apache cxf and Maven to develop Web Services

Source: Internet
Author: User

Currently, the main java WebService framework is left with axis2 and cxf. This article compares the objectives, standard support, development, and deployment of the two frameworks. We recommend that you use cxf to publish WebService in existing web applications. Furthermore, this article introduces the embedded cxfCodeAnd web containers.

The example in this article is built using Maven.

Table of contents
    • 1 Comparison Between axis2 and cxf
    • 2. Write a service class
    • 3. Publish with an endpoint
    • 4 publish in webapp
1 Comparison between axis2 and cxf

The release of JWS has a huge impact on the Java WebService framework. After the great waves, the current Java WebService development frameworks mainly include axis2 and cxf.

Both axis2 and cxf are Apache products, but they have different purposes, resulting in different WebService development methods. Both frameworks are supported by developers. It is necessary to compare the two.

  Axis2 Cxf
Target WebService Engine A simple SOA framework can be used as an ESB
WS * Standard Support WS-policy is not supported. WS-Addressing, WS-Policy, WS-RM, WS-Security, WS-I basic profile
Data Binding support Xmlbeans, jibx, jaxme, jaxbri, ADB Jaxb, Aegis, xmlbeans, SDO, jibx
Spring Integration Not Supported Supported
Application Integration Difficult Simple
Multiple Languages Supports C/C ++ Not Supported
Deployment Web Applications Embedded
Service Monitoring and Management Supported Not Supported

Conclusion:

    1. If you want to implement WebService in a consistent way, especially when there is a cross-language requirement, you should use axis2
    2. If you needProgram(Including web applications) to add WebService support, you should use cxf
2 Write Service Classes

Since Java 6, the WebService API has been copied from Java EE to Java SE. And follow a series of standards, such as jsr181 (Web Service metadata), jsr224 (JAX-WS, XML-based WebService API), jsr67 (SAAJ, soap attachment standard) and so on. It is defined in the javax. JWS, javax. xml. ws, and javax. xml. Soap packages respectively.

Jsr181 supports defining WebService using annotation. The main annotation classes in javax. JWS include:

Annotation Description
WebService Mark Java classes as implementing Web Services, or mark Java interfaces as defining web service interfaces.
Webmethod Customize web service methods
Webparam Customize web service method parameters
Webresult Customize the returned values of web service methods
Soapbinding Specify the soap ing style of WebService

Annotations allow external code to obtain more metadata without changing the code logic. The following uses the annotation defined by javax. JWS to declare a WebService:

    • Create a Maven Project
MVN archetype: Create-dgroupid = com. mycompany-dartifactid = cxfdemo-darchetypeartifactid = Maven-Archetype-webapp

 

    • Add cxf dependency
 <  Dependency  >          <  Groupid  > Org. Apache. cxf </  Groupid  >          <  Artifactid  > Apache-cxf </ Artifactid  >          <  Version  > $ {Cxf. Version} </  Version  >          <  Type  > POM </  Type  >    </  Dependency > 
    • Configure the jetty plug-in
 <  Build  >                  <  Plugins  >              <  Plugin  >                  <  Groupid  > Org. mortbay. Jetty </  Groupid  >                 <  Artifactid  > Maven-jetty-plugin </  Artifactid  >              </  Plugin  >          </  Plugins  >      </  Build  > 

 

    • Create a Service Interface
PackageCxfdemo;ImportJavax. JWS. WebService; @ WebServicePublic InterfaceCxfdemo {PublicString sayhello (string Foo );}
    • Implementation Service
PackageCxfdemo;ImportJavax. JWS. WebService; @ WebService ()Public ClassCxfdemoimplImplementsCxfdemo {PublicString sayhello (string Foo ){Return"Hello" +Foo ;}}
3 Publish with endpoint

So far, everything in standard Java SE has been used. The following describes how to use cxf to implement some functions.

The first is service release. Cxf supports not only publishing WebService through web containers, but also publishing WebService through jetty in embedded code.

The following test class contains the Code for publishing the service and calling the client:

 Package Cxfdemo. test;  Import  Javax. xml. ws. endpoint;  Import  JUnit. Framework. Assert;  Import  JUnit. Framework. testcase;  Import  Org. Apache. cxf. jaxws. jaxwsproxyfactorybean;  Import  Cxfdemo. cxfdemo;  Import  Cxfdemo. cxfdemoimpl;  Public   Class Testendpoint Extends  Testcase {  Private   Static   Final String address = "http: // localhost: 9000/cxfdemo" ;  Protected   Void Setup () Throws  Exception {  Super  . Setup (); system. Out. println ( "Starting server" ); Cxfdemoimpl demo =New  Cxfdemoimpl (); endpoint. Publish (address, demo); system. Out. println ( "Start success" );}  Public   Void  Testsayhello () {jaxwsproxyfactorybean Factory = New  Jaxwsproxyfactorybean (); factory. setserviceclass (cxfdemo.  Class  ); Factory. setaddress (Address); cxfdemo Client =(Cxfdemo) Factory. Create (); Assert. assertequals (client. sayhello ( "Foo"), "Hello foo" );}} 

The test result is as follows:

 $ MVN test ...... ------------------------------------------------------- T e S t s ------------------------------------------------------- Running cxfdemo. Test. testendpointstarting Server  2012 - 12 - 12   11 : 29 : 02 Org. Apache. cxf. Service. Factory. reflectionservicefactorybean buildservicefromclass ?? Failed: creating service {http: //  Cxfdemo/} cxfdemoimplservice from class cxfdemo. cxfdemo  2012 - 12 - 12   11 : 29 : 03  Org. Apache. cxf. endpoint. serverimpl initdestination ?? Setting: Setting the server ' S publish address to be http: // localhost: 9000/cxfdemo  2012 - 12 - 12   11 : 29 : 04 Org. Eclipse. Jetty. util. log. slf4jlog Info ?? Usage: jetty- 7.4 . 2  . V20110526  2012 - 12 - 12  11 : 29 : 04 Org. Eclipse. Jetty. util. log. slf4jlog Info ?? Login: started selectchannelconnector @ localhost: 9000  Starting  2012 - 12 - 12   11 : 29 : 04 Org. Eclipse. Jetty. util. log. slf4jlog Info ?? Usage: started O. E. J. S. H. contexthandler {, Null  } Start success  2012 - 12 - 12   11 : 29 : 04  Org. Apache. cxf. Service. Factory. reflectionservicefactorybean buildservicefromclass ?? Failed: creating service {http: //  Cxfdemo/} cxfdemoservice from class cxfdemo. cxfdemo Tests run: 1 , Failures:0 , Errors: 0 , Skipped: 0 , Time elapsed: 3.076  Secresults: tests run:  1 , Failures: 0 , Errors: 0 , Skipped: 0  ...... 
4 Publish in webapp

Cxf provides spring integration and org. Apache. cxf. Transport. servlet. cxfservlet for publishing WebService in Web containers. In the preceding example, the entire APACHE-cxf dependency is added, so the reference to srping is automatically added. You only need to write the beans configuration file and the web. xml file.

    • Configure cxfservlet in Web. xml
  <  Servlet  >          <  Servlet-name  > Cxfservlet </  Servlet-name  >          <  Servlet-class  > Org. Apache. cxf. Transport. servlet. cxfservlet </  Servlet-class >      </  Servlet  >      <  Servlet-Mapping  >          <  Servlet-name  > Cxfservlet </  Servlet-name  >          <  URL-Pattern  > /Services /*</  URL-Pattern  >      </  Servlet-Mapping  > 

 

    • Add the contextloaderlistener of spring in Web. xml and configure context-Param
  <  Context-Param  >           <  Param-name  > Contextconfiglocation </  Param-name >          <  Param-Value  > /WEB-INF/cxfdemo-beans.xml </  Param-Value  >      </  Context-Param  >      <  Listener  >          <  Listener-class  > Org. springframework. Web. Context. contextloaderlistener </  Listener-class  >      </  Listener  > 

 

    • The contents of the beans configuration file are as follows:

Cxfdemo-beans.xml

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Beans  Xmlns  = "Http://www.springframework.org/schema/beans" Xmlns: xsi  = "Http://www.w3.org/2001/XMLSchema-instance"  Xmlns: jaxws  = "Http://cxf.apache.org/jaxws"  Xsi: schemalocation  = "Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"  >      <  Jaxws: endpoint  ID  = "Cxfdemo"  Implementor = "Cxfdemo. cxfdemoimpl"  Address  = "/Cxfdemo"   />  </  Beans  > 

 

In this way, WebService has been released in the Web container. Start a web application:

 
$ MVN jetty: Run

 

You can see the published WebService in the browser, for example:

Date: 2012-12-12 15:56:07 CST

Author: Holbrook

Org version 7.8.11 with Emacs Version 24

Validate XHTML 1.0

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.