Enterprise Integration Zone is brought to you in partnership with:

Lijin Joseji is a Senior IT Specialist working with IBM Global Business Services since 2008. He has been involved in different projects which make use of WebSphere eXtream deployment components as well as other open source technologies. His areas of expertise includes design and development of J2EE applications, WebSphere eXtream Deployment Components such as IBM Object Grid, Compute Grid, SOA Architecture, open source frameworks such as Spring, Hibernate, Web service frameworks, NoSQL & SQL Databases and mobile development. Currently he works and specializes in WebSphere eXtreme Scale and WebSphere Extended Deployment Compute Grid and Object Grid related Projects, NoSQL dabatases, Android Development and Cloud computing. He used to write his technical views and experience through his Blog called OrangeSlate.com. Lijin is a DZone MVB and is not an employee of DZone and has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

Creating Web services using Apache CXF: Development

07.18.2012
| 6732 views |
  • submit to reddit

We need to set-up the project  environment first. Please download the following JARs.

Versions Used :

CXF 2.2.2           Download Link : CXF Site Download Link

Spring 2.5.6       Download Link : SpringSource Download site

The following jars are required for all CXF usage: But you will be getting it through CXF Download.
- cxf.jar
- commons-logging.jar
- geronimo-activation.jar (Or the Sun equivalent) [6]
- geronimo-annotation.jar (Or the Sun equivalent) [6]
- geronimo-javamail.jar (Or the Sun equivalent) [6]
- geronimo-stax-api.jar (Or the Sun equivalent) [6]
- neethi.jar
- jaxb-api.jar  [6]
- jaxb-impl.jar
- XmlSchema.jar
- wstx-asl.jar  [6]
- wsdl4j.jar

The following jars are required for XML catalog support:
- xml-resolver.jar

For Java2WSDL and WSDL2Java:
- The above jars
- jaxb-xjc.jar
- velocity.jar
- commons-collections.jar
- commons-lang.jar

For JAX-WS support:
- geronimo-ws-metadata.jar [6]
- geronimo-jaxws_2.1_spec-1.0.jar (Or the Sun equivalent) [6]
- saaj-api.jar [6]
- saaj-impl.jar [6]
- asm.jar (semi-optional, helps with performance of wrapper types and is
        required when adding JAXB annotations onto the SEI methods and
    parameters.)

For XML Configuration support:
- aopalliance.jar
- spring-beans.jar
- spring-context.jar
- spring-core.jar
- spring.web.jar
- FastInfoset.jar

For standalone HTTP support:
- geronimo-servlet.jar
- jetty.jar
- jetty-util.jar
- sl4j.jar & sl4j-jdk14.jar (optional – but improves logging)
For Aegis support:
- jdom.jar (optional, if you want to map xsd:anyType to JDOM)

For WS-Security support:
- bcprov-jdk15.jar
- xalan.jar
- serializer.jar
- wss4j.jar
- xmlsec.jar

For HTTP Binding support:
- jra.jar
- jettison.jar (Needed for JSON services only)

For JAX-RS support:
- abdera*
- axiom*
- jsr311-api.jar
- jettison.jar (Needed for JSON services only)

For JMS transport
- geronimo-jms.jar (Or the Sun equivalent)
- spring-jms.jar

For CORBA support:
- antlr.jar

If you want to use Aegis databinding which is providing by CXF then:

Download :jdom-1.0.jar

For Spring you can download its latest version say 2.5.

Step 2: Creating a Project

  • Open your IDE ( I am using Eclipse) and create a Web project. Lets say with Name CXFExample.

Project

  • Create a package say “com.your.company.service”
  • Create a POJO class with a name say “Product”. And include some properties like productName etc.
   1: package com.your.company.service;

   2:  

   3: import org.apache.cxf.aegis.type.java5.IgnoreProperty;

   4:  

   5: public final class Product {

   6:     private String itemName;

   7:     private String category;

   8:     private double price;

   9:     private String details;

  10:  

  11:     public Product() {

  12:     }

  13:  

  14:     public Product(String itemName, String category, double price,

  15:             String details) {

  16:         this.itemName = itemName;

  17:         this.category = category;

  18:         this.price = price;

  19:         this.details = details;

  20:     }

  21:  

  22:     public String getItemName() {

  23:         return itemName;

  24:     }

  25:  

  26:     public void setItemName(String itemName) {

  27:         this.itemName = itemName;

  28:     }

  29:  

  30:     public String getCategory() {

  31:         return category;

  32:     }

  33:  

  34:     public void setCategory(String category) {

  35:         this.category = category;

  36:     }

  37:  

  38:     public double getPrice() {

  39:         return price;

  40:     }

  41:  

  42:     public void setPrice(double price) {

  43:         this.price = price;

  44:     }

  45:  

  46:     @IgnoreProperty

  47:     public String getDetails() {

  48:         return details;

  49:     }

  50:  

  51:     public void setDetails(String details) {

  52:         this.details = details;

  53:     }

  54:  

  55: }

Note that we used one Annotation from Aegis. Its just to get some idea about that annotation. As you know Aegis is using for data binding. As the annotation denotes if we are mentioning it is not needed in the WSDL file.

    Creating the Service Interface

   1: package com.your.company.service;

   2:  

   3: import java.util.List;

   4:  

   5: import javax.jws.WebParam;

   6: import javax.jws.WebService;

   7:  

   8: @WebService

   9: public interface ProductService {

  10:  

  11:     List<Product> getProducts();

  12:  

  13:     void addProduct(@WebParam(name = "product") Product product);

  14:  

  15:     public void addProducts(@WebParam(name = "products") List<Product> products);

  16:  

  17: }

Here we are using @WebService annotation and @WebParam Annotations. They are JAX-WS annotations.

@WebService is using to denote this Interface as the Web Service Interface. So while creating WSDL file this will consider.

@WebParam is using to name the Arguments in the WSDL file. Default it will be like Arg0, Arg1 etc. But if we are giving some names then that will be more identifiable in the WSDL.

  • Create the Service Implementation class

 

1: package com.your.company.service;

   2:  

   3: import java.util.ArrayList;

   4: import java.util.List;

   5:  

   6: import javax.jws.WebParam;

   7: import javax.jws.WebService;

   8:  

   9: @WebService(endpointInterface = "edu.web.service.ProductService")

  10: public final class ProductServiceImpl implements ProductService {

  11:  

  12:     public List<Product> getProducts() {

  13:         List<Product> products = new ArrayList<Product>();

  14:         products.add(new Product("SpringInAction", "Manning", 200,

  15:                 "Book about Spring"));

  16:         products.add(new Product("EJB3InAction", "Manning", 200,

  17:                 "Book about EJB3"));

  18:         return products;

  19:     }

  20:  

  21:     @Override

  22:     public void addProduct(Product product) {

  23:         System.out.println(product);

  24:     }

  25:  

  26:     @Override

  27:     public void addProducts(@WebParam(name = "products") List<Product> products) {

  28:         for (Product product : products) {

  29:             System.out.println(product);

  30:         }

  31:     }

  32: }

Here we are writing the Impls of our Interface methods. @WebService annotation is marking this class as a web service implementation

In the next PART we will be discussing about the Configurations.

 

Published at DZone with permission of Lijin Joseji, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)