Showing posts with label CXF. Show all posts
Showing posts with label CXF. Show all posts

Friday, April 13, 2012

Web Service: Apache CXF Integration with Hibernate Tutorial

Overview:
This tutorial will look at how you can integrate Hibernate with Apache CXF. This article is an extension of both Web Service: Apache CXF Web Service Tutorial and Spring: Spring ROM Support for Hibernate with MySQL articles.

You can find the sample source code here.

Prerequisite:
MySQL
Maven
JDK >= 1.5.x

Project Structure: This is how the project structure looks like (Click on the image to view)


Step 1: Creating the Database Table
 CREATE TABLE PERSON (
   PERSON_ID BIGINT AUTO_INCREMENT,
   FIRST_NAME VARCHAR(30),
   LAST_NAME VARCHAR(30),
   PRIMARY KEY (PERSON_ID)
)

Step 2: Maven Dependencies (pom.xml) 
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
   <modelVersion>4.0.0</modelVersion>  
   <groupId>org.fazlan</groupId>  
   <artifactId>org.fazlan.employee.cxfspring.service</artifactId>  
   <packaging>war</packaging>  
   <version>1.0-SNAPSHOT</version>  
   <name>org.fazlan.employee.cxfspring.service</name>  
   <url>http://maven.apache.org</url>  
   <build>  
     <finalName>CXFEmployeeHBMService</finalName>  
     <plugins>  
       <plugin>  
         <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-compiler-plugin</artifactId>  
         <configuration>  
           <source>1.6</source>  
           <target>1.6</target>  
         </configuration>  
       </plugin>  
     </plugins>  
   </build>  
   <dependencies>  
     <!-- JUnit testing framework -->  
     <dependency>  
       <groupId>junit</groupId>  
       <artifactId>junit</artifactId>  
       <version>4.8.1</version>  
     </dependency>  
     <!-- CXF dependencies -->  
     <dependency>  
       <groupId>org.apache.cxf</groupId>  
       <artifactId>cxf-rt-frontend-jaxws</artifactId>  
       <version>${cxf.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.cxf</groupId>  
       <artifactId>cxf-rt-transports-http</artifactId>  
       <version>${cxf.version}</version>  
     </dependency>  
     <!-- Spring framework -->  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-core</artifactId>  
       <version>${org.springframework.version}</version>  
     </dependency>  
     <!--<dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-context</artifactId>  
       <version>${org.springframework.version}</version>  
     </dependency>-->  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-aop</artifactId>  
       <version>${org.springframework.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-beans</artifactId>  
       <version>${org.springframework.version}</version>  
     </dependency>  
     <!--<dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-context-support</artifactId>  
       <version>${org.springframework.version}</version>  
     </dependency>-->  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-tx</artifactId>  
       <version>${org.springframework.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-jdbc</artifactId>  
       <version>${org.springframework.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-orm</artifactId>  
       <version>${org.springframework.version}</version>  
     </dependency>  
     <!-- MySQL database driver -->  
     <dependency>  
       <groupId>mysql</groupId>  
       <artifactId>mysql-connector-java</artifactId>  
       <version>5.1.9</version>  
     </dependency>  
     <!-- Hibernate framework -->  
     <dependency>  
       <groupId>org.hibernate</groupId>  
       <artifactId>hibernate-core</artifactId>  
       <version>${org.hibernate.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.hibernate</groupId>  
       <artifactId>hibernate-entitymanager</artifactId>  
       <version>${org.hibernate.version}</version>  
     </dependency>  
     <!-- Hibernate library dependecy start -->  
     <!--<dependency>  
       <groupId>dom4j</groupId>  
       <artifactId>dom4j</artifactId>  
       <version>1.6.1</version>  
     </dependency>  
     <dependency>  
       <groupId>commons-logging</groupId>  
       <artifactId>commons-logging</artifactId>  
       <version>1.1.1</version>  
     </dependency>  
     <dependency>  
       <groupId>commons-collections</groupId>  
       <artifactId>commons-collections</artifactId>  
       <version>3.2.1</version>  
     </dependency>  
     <dependency>  
       <groupId>antlr</groupId>  
       <artifactId>antlr</artifactId>  
       <version>2.7.7</version>  
     </dependency>-->  
     <!-- Hibernate library dependency end -->  
     <!--AspectJ dependency-->  
     <dependency>  
       <groupId>org.aspectj</groupId>  
       <artifactId>aspectjrt</artifactId>  
       <version>${org.aspectj.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.aspectj</groupId>  
       <artifactId>aspectjweaver</artifactId>  
       <version>${org.aspectj.version}</version>  
     </dependency>  
   </dependencies>  
   <properties>  
     <org.apache.axis2.version>1.6.1</org.apache.axis2.version>  
     <org.aspectj.version>1.6.11</org.aspectj.version>  
     <cxf.version>2.2.3</cxf.version>  
     <org.hibernate.version>3.6.0.Final</org.hibernate.version>  
     <org.springframework.version>3.1.1.RELEASE</org.springframework.version>  
   </properties>  
 </project>  

Step 3: Creating POJO Classes and Service Implementation
Employee.java
 package org.fazlan.employee.cxf.hibernate.service.beans;  
 import javax.xml.bind.annotation.XmlRootElement;  
 import javax.xml.bind.annotation.XmlType;  
 import java.io.Serializable;  
 @XmlType(  
     name = "EmployeeType",  
     namespace = "org.fazlan.employee.cxf.hibernate.service.beans",  
     propOrder = {"id", "firstName", "lastName"})  
 @XmlRootElement(name = "Employee")  
 public class Employee implements Serializable {  
   private Long id;  
   private String firstName;  
   private String lastName;  
   public Long getId() {  
     return id;  
   }  
   public void setId(Long id) {  
     this.id = id;  
   }  
   public String getFirstName() {  
     return firstName;  
   }  
   public void setFirstName(String firstName) {  
     this.firstName = firstName;  
   }  
   public String getLastName() {  
     return lastName;  
   }  
   public void setLastName(String lastName) {  
     this.lastName = lastName;  
   }  
 }  

EmployeeCollection.java - This is used to hold a collection object of Employee.java
 package org.fazlan.employee.cxf.hibernate.service.beans;  
 import javax.xml.bind.annotation.XmlElement;  
 import javax.xml.bind.annotation.XmlElementWrapper;  
 import javax.xml.bind.annotation.XmlRootElement;  
 import javax.xml.bind.annotation.XmlType;  
 import java.util.List;  
 @XmlType(  
     name = "EmployeeCollectionType",  
     namespace = "org.fazlan.employee.cxf.hibernate.service.beans")  
 @XmlRootElement(name = "EmployeeCollection")  
 public class EmployeeCollection {  
   private List<Employee> employees;  
   public EmployeeCollection() {  
   }  
   public EmployeeCollection(List<Employee> employees) {  
     this.employees = employees;  
   }  
   @XmlElementWrapper(name = "employees")  
   @XmlElement(name = "employee")  
   public List<Employee> getEmployees() {  
     return employees;  
   }  
 }  

IDao.java - DAO Interface
 package org.fazlan.employee.cxf.hibernate.service.dao;  
 import java.util.List;  
 public interface IDao<T> {  
   Long save(T t);  
   void update(T t);  
   void delete(T t);  
   T get(Long id);  
   List<T> getAll();  
 }  

EmployeeDao.java - The implementation of employee DAO using HibernateDao support.
 package org.fazlan.employee.cxf.hibernate.service.dao;  
 import org.fazlan.employee.cxf.hibernate.service.beans.Employee;  
 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
 import java.util.List;  
 public class EmployeeDao extends HibernateDaoSupport implements IDao<Employee>{  
   public Long save(Employee employee) {  
     return (Long) getHibernateTemplate().save(employee);  
   }  
   public void update(Employee employee) {  
     getHibernateTemplate().update(employee);  
   }  
   public void delete(Employee employee) {  
     getHibernateTemplate().delete(employee);  
   }  
   public Employee get(Long id) {  
     return getHibernateTemplate().get(Employee.class, id);  
   }  
   public List<Employee> getAll() {  
     return getHibernateTemplate().find("from Employee");  
   }  
 }  

IEmployeeService.java - Service Interface.
 package org.fazlan.employee.cxf.hibernate.service.services;  
 import org.fazlan.employee.cxf.hibernate.service.beans.Employee;  
 import org.fazlan.employee.cxf.hibernate.service.beans.EmployeeCollection;  
 import javax.jws.WebMethod;  
 import javax.jws.WebParam;  
 import javax.jws.WebResult;  
 import javax.jws.WebService;  
 @WebService(targetNamespace = "org.fazlan.employee.cxf.hibernate.service.services")  
 public interface IEmployeeService {  
   @WebMethod(operationName = "AddEmployee")  
   @WebResult(name = "id")  
   Long add(@WebParam(name = "employee") Employee employee);  
   @WebMethod(operationName = "UpdateEmployee")  
   void update(@WebParam(name = "employee") Employee employee);  
   @WebMethod(operationName = "GetEmployee")  
   @WebResult(name = "employee")  
   Employee get(@WebParam(name = "id") Long id);  
   @WebMethod(operationName = "GetAllEmployees")  
   EmployeeCollection getAll();  
 }  

EmployeeService.java - Implementation of the service
 package org.fazlan.employee.cxf.hibernate.service.services;  
 import org.fazlan.employee.cxf.hibernate.service.beans.Employee;  
 import org.fazlan.employee.cxf.hibernate.service.beans.EmployeeCollection;  
 import org.fazlan.employee.cxf.hibernate.service.dao.IDao;  
 import javax.jws.WebMethod;  
 import javax.jws.WebService;  
 @WebService(  
     endpointInterface = "org.fazlan.employee.cxf.hibernate.service.services.IEmployeeService",  
     targetNamespace = "org.fazlan.employee.cxf.hibernate.service.services",  
     serviceName = "EmployeeService")  
 public class EmployeeService implements IEmployeeService {  
   private IDao<Employee> employeeDao;  
   public Long add(Employee employee) {  
     return employeeDao.save(employee);  
   }  
   public void update(Employee employee) {  
     employeeDao.update(employee);  
   }  
   public Employee get(Long id) {  
     return employeeDao.get(id);  
   }  
   public EmployeeCollection getAll() {  
     return new EmployeeCollection(employeeDao.getAll());  
   }  
   @WebMethod(exclude = true)  
   public void setEmployeeDao(IDao<Employee> employeeDao) {  
     this.employeeDao = employeeDao;  
   }  
 }  

Step 4: Mapping the POJO classes using Hibernate (hibernate/hbm/Employee.hbm.xml)
 <?xml version="1.0"?>  
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
 <hibernate-mapping>  
   <class name="org.fazlan.employee.cxf.hibernate.service.beans.Employee" table="PERSON">  
     <id name="id" column="PERSON_ID" type="long" access="field">  
       <generator class="increment" />  
     </id>  
     <property name="firstName" type="string">  
       <column name="FIRST_NAME" />  
     </property>  
     <property name="lastName" type="string">  
       <column name="LAST_NAME" />  
     </property>  
   </class>  
 </hibernate-mapping>  

Step 5: Defining the database properties (hibernate/properties/db.properties)
 jdbc.driverClassName=com.mysql.jdbc.Driver  
 jdbc.url=jdbc:mysql://localhost:3306/springhibernate  
 jdbc.username=root  
 jdbc.password=root123  

Step 6: Spring Configuration (spring/spring-emp-*.xml) 

spring/spring-emp-dao.xml - Contains the Spring configuration for the DataSource, SessionFactory, EmployeeDao and Transaction Management.
 <beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xmlns:aop="http://www.springframework.org/schema/aop"  
     xmlns:tx="http://www.springframework.org/schema/tx"  
     xsi:schemaLocation="  
 http://www.springframework.org/schema/beans  
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 http://www.springframework.org/schema/tx  
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
 http://www.springframework.org/schema/aop  
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
   <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
     <property name="location" value="classpath:hibernate/properties/db.properties"/>  
   </bean>  
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
     <property name="url" value="${jdbc.url}"/>  
     <property name="driverClassName" value="${jdbc.driverClassName}"/>  
     <property name="username" value="${jdbc.username}"/>  
     <property name="password" value="${jdbc.password}"/>  
   </bean>  
   <!-- Hibernate session factory -->  
   <bean id="sessionFactory"  
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
     <property name="dataSource">  
       <ref bean="dataSource"/>  
     </property>  
     <property name="hibernateProperties">  
       <props>  
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
         <prop key="hibernate.show_sql">true</prop>  
       </props>  
     </property>  
     <property name="mappingLocations">  
         <value>classpath:hibernate/hbm/Employee.hbm.xml</value>  
     </property>  
   </bean>  
   <bean id="employeeDao" class="org.fazlan.employee.cxf.hibernate.service.dao.EmployeeDao" >  
       <property name="sessionFactory" ref="sessionFactory" />  
   </bean>  
   <!-- transaction management -->  
   <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
     <property name="sessionFactory" ref="sessionFactory"/>  
   </bean>  
   <tx:advice id="txAdvice" transaction-manager="txManager">  
     <tx:attributes>  
       <tx:method name="get" read-only="true"/>  
       <tx:method name="*"/>  
     </tx:attributes>  
   </tx:advice>  
   <aop:config>  
     <aop:pointcut id="serviceOperationsPC"  
            expression="execution(* org.fazlan.employee.cxf.hibernate.service.services.IEmployeeService.*(..))"/>  
     <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperationsPC"/>  
   </aop:config>  
 </beans>  

spring/spring-emp-app-context.xml - Contains the Spring configuration for the Service and it's endpoint.
 <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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
   <import resource="classpath:META-INF/cxf/cxf.xml"/>  
   <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
   <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
   <import resource="classpath:spring/spring-emp-dao.xml"/>  
   <jaxws:endpoint id="employeeServiceEPR" implementor="#employeeService" address="/employeeservice"/>  
   <bean id="employeeService" class="org.fazlan.employee.cxf.hibernate.service.services.EmployeeService">  
     <property name="employeeDao" ref="employeeDao"/>  
   </bean>  
 </beans>  

Step 7: Updating the web.xml to Register the CXF Servlet.
 <!DOCTYPE web-app PUBLIC  
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
 <web-app>  
  <display-name>CXF Web Service Web Application</display-name>  
   <context-param>  
     <param-name>contextConfigLocation</param-name>  
     <param-value>classpath*:spring/spring-emp-app-context.xml</param-value>  
   </context-param>  
   <listener>  
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
     </listener>  
   <servlet>  
     <servlet-name>CXFServlet</servlet-name>  
     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
     <load-on-startup>1</load-on-startup>  
   </servlet>  
   <servlet-mapping>  
     <servlet-name>CXFServlet</servlet-name>  
     <url-pattern>/*</url-pattern>  
   </servlet-mapping>  
 </web-app>  

Step 8: Building the Project
    mvn clean install
Step 9: Deploying the Service
Now you can deploy this service on to any container. Here we'll deploy this on Tomcat server.

Step 10: Testing the Web Service
After successfully deploying the service, you can access the service via http://localhost:8080/CXFEmployeeHBMService/employeeservice?wsdl

Use this URL to create a SOAP Project as follows.

 
Summary:
With Apache CXF, you can easily integrate Hibernate with a matter of minutes. This is one of the advantages Apache CXF has over Apache Axis2 when it comes to integrating Hibernate with Spring.
You can find the sample source code here.

Thursday, April 12, 2012

Part 2: RESTful WS with Apache CXF - JSON

Overview:
This is Part 2 of the article on RESTful WS with Apache CXF. If you haven't looked at Part 1, I suggest you to do so.

This article looks at how you can do JSON/HTTP based RESTful WS with Apache CXF. We're using the CXF support on JAX-RS (JSR-311) for this. The sample code can be found here.

Prerequisite:JDK >= 1.5.x
Maven >= 2.2.x

Project Structure: Following is the project structure I have used (Maven WAR project).


Step 1: Maven Dependencies (pom.xml)
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <groupId>org.fazlan</groupId> 
  <artifactId>org.fazlan.employee.cxf.jsonrest.service</artifactId> 
  <packaging>war</packaging> 
  <version>1.0-SNAPSHOT</version> 
  <name>org.fazlan.employee.cxf.jsonrest.service</name> 
  <url>http://maven.apache.org</url> 
  <build> 
    <finalName>CXFEmployeeJsonRESTService</finalName> 
    <plugins> 
      <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <configuration> 
          <source>1.6</source> 
          <target>1.6</target> 
        </configuration> 
      </plugin> 
    </plugins> 
  </build> 
  <dependencies> 
    <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
    </dependency> 
    <!-- Spring framework --> 
    <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>${org.springframework.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${org.springframework.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.8.1</version> 
    </dependency> 
    <dependency> 
      <groupId>org.apache.cxf</groupId> 
      <artifactId>cxf-rt-frontend-jaxrs</artifactId> 
      <version>${cxf.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.apache.cxf</groupId> 
      <artifactId>cxf-rt-databinding-aegis</artifactId> 
      <!-- 2.4.4 or 2.5.0 --> 
      <version>${cxf.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.apache.xmlbeans</groupId> 
      <artifactId>xmlbeans</artifactId> 
      <version>2.4.0</version> 
    </dependency> 
  </dependencies> 
  <properties> 
    <org.springframework.version>3.1.1.RELEASE</org.springframework.version> 
    <cxf.version>2.4.6</cxf.version> 
  </properties> 
</project> 

Step 2: Creating POJO Classes and Service Implementation

Employee.java
 package org.fazlan.employee.cxf.jsonrest.service.beans; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 
import java.io.Serializable; 
@XmlType( 
    name = "EmployeeType", 
    namespace = "org.fazlan.employee.cxf.jsonrest.service.beans", 
    propOrder = {"id", "firstName", "lastName"}) 
@XmlRootElement( 
    name = "Employee", 
    namespace = "org.fazlan.employee.cxf.jsonrest.service.beans") 
public class Employee implements Serializable { 
  private Long id; 
  private String firstName; 
  private String lastName; 
  public Employee() { 
  } 
  public Employee(Long id, String firstName, String lastName) { 
    this.id = id; 
    this.firstName = firstName; 
    this.lastName = lastName; 
  } 
  public Long getId() { 
    return id; 
  } 
  public void setId(Long id) { 
    this.id = id; 
  } 
  public String getFirstName() { 
    return firstName; 
  } 
  public void setFirstName(String firstName) { 
    this.firstName = firstName; 
  } 
  public String getLastName() { 
    return lastName; 
  } 
  public void setLastName(String lastName) { 
    this.lastName = lastName; 
  } 
  public boolean equals(Object o) { 
    if (this == o) return true; 
    if (!(o instanceof Employee)) return false; 
    Employee employee = (Employee) o; 
    return !(firstName != null ? !firstName.equals(employee.firstName) : employee.firstName != null) && 
        !(id != null ? !id.equals(employee.id) : employee.id != null) && 
        !(lastName != null ? !lastName.equals(employee.lastName) : employee.lastName != null); 
  } 
  public int hashCode() { 
    int result = id != null ? id.hashCode() : 0; 
    result = 31 * result + (firstName != null ? firstName.hashCode() : 0); 
    result = 31 * result + (lastName != null ? lastName.hashCode() : 0); 
    return result; 
  } 
  @Override 
  public String toString() { 
    return "Employee{" + 
        "id=" + id + 
        ", firstName='" + firstName + '\'' + 
        ", lastName='" + lastName + '\'' + 
        '}'; 
  } 
} 

Employees.java - This is used to hold a collection object of Employee.java
 package org.fazlan.employee.cxf.jsonrest.service.beans; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlElementWrapper; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 
import java.util.Collection; 
@XmlType( 
    name = "EmployeesType", 
    namespace = "org.fazlan.employee.cxf.jsonrest.service.beans") 
@XmlRootElement( 
    name = "Employees", 
    namespace = "org.fazlan.employee.cxf.jsonrest.service.beans") 
public class Employees { 
  private Collection<Employee> employees; 
  public Employees() { 
  } 
  public Employees(Collection<Employee> employees) { 
    setEmployees(employees); 
  } 
  @XmlElement(name = "employee",required = true) 
  @XmlElementWrapper(name = "employees") 
  public Collection<Employee> getEmployees() { 
    return employees; 
  } 
  public void setEmployees(Collection<Employee> employees) { 
    this.employees = employees; 
  } 
} 

IEmployeeService.java - Service Interface
 package org.fazlan.employee.cxf.jsonrest.service.services; 
import org.fazlan.employee.cxf.jsonrest.service.beans.Employee; 
import org.fazlan.employee.cxf.jsonrest.service.beans.Employees; 
import javax.ws.rs.core.Response; 
public interface IEmployeeService { 
  Employee get(Long id); 
  Employees getAll(); 
  Response add(Employee employee); 
  Response update(Employee employee); 
  Response delete(Long id); 
} 


EmployeeService.java - Implementation of the service.

NOTE: This service class has been annotated to produce and consume certain methods in JSON(application/json) as the mime-type. Read more about Apache CXF - JAX-RS basics.

 package org.fazlan.employee.cxf.jsonrest.service.services; 
import org.fazlan.employee.cxf.jsonrest.service.beans.Employee; 
import org.fazlan.employee.cxf.jsonrest.service.beans.Employees; 
import javax.ws.rs.*; 
import javax.ws.rs.core.Response; 
import java.util.HashMap; 
import java.util.Map; 
@Path("/employeeservice/") 
@Produces("application/json") 
public class EmployeeService implements IEmployeeService { 
  private static final Map<Long, Employee> EMPLOYEE_MAP = new HashMap<Long, Employee>(); 
  private static long index = 3L; 
  static { 
    EMPLOYEE_MAP.put(1L, new Employee(1L, "First Name 1", "Last Name 1")); 
    EMPLOYEE_MAP.put(2L, new Employee(2L, "First Name 2", "Last Name 2")); 
  } 
  @GET 
  @Path("/get/{id}") 
  public Employee get(@PathParam("id") Long id) { 
    return EMPLOYEE_MAP.get(id); 
  } 
  @GET 
  @Path("/getall/") 
  public Employees getAll() { 
    return new Employees(EMPLOYEE_MAP.values()); 
  } 
  @POST 
  @Path("/add/") 
  @Consumes("application/json") 
  public Response add(Employee employee) { 
    System.out.println("Adding :" + employee); 
    employee.setId(index++); 
    update(employee); 
    return Response.status(Response.Status.OK).build(); 
  } 
  @PUT 
  @Path("/update/") 
  @Consumes("application/json") 
  public Response update(Employee employee) { 
    System.out.println("Updating :" + employee); 
    EMPLOYEE_MAP.put(employee.getId(), employee); 
    return Response.status(Response.Status.OK).build(); 
  } 
  @DELETE 
  @Path("/delete/{id}/") 
  public Response delete(@PathParam("id") Long id) { 
    Employee e = EMPLOYEE_MAP.remove(id); 
    System.out.println("Deleted :" + e); 
    return Response.status(Response.Status.OK).build(); 
  } 
} 


Read more about Apache CXF - JAX-RS basics.

Step 3: Exposing the Service Beans as RESTful Web Service
 <?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:jaxrs="http://cxf.apache.org/jaxrs" 
    xmlns:cxf="http://cxf.apache.org/core" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans.xsd 
              http://cxf.apache.org/jaxrs 
              http://cxf.apache.org/schemas/jaxrs.xsd 
              http://cxf.apache.org/core 
              http://cxf.apache.org/schemas/core.xsd"> 
  <!-- do not use import statements if CXFServlet init parameters link to this beans.xml --> 
  <import resource="classpath:META-INF/cxf/cxf.xml"/> 
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> 
  <jaxrs:server id="employeeService" address="/"> 
    <jaxrs:serviceBeans> 
      <ref bean="employeeServiceBean"/> 
    </jaxrs:serviceBeans> 
    <jaxrs:extensionMappings> 
      <entry key="xml" value="application/xml" /> 
      <entry key="json" value="application/json" /> 
    </jaxrs:extensionMappings> 
    <jaxrs:features> 
      <cxf:logging/> 
    </jaxrs:features> 
  </jaxrs:server> 
  <bean id="employeeServiceBean" class="org.fazlan.employee.cxf.jsonrest.service.services.EmployeeService"/> 
</beans> 

Step 4: Updating the web.xml to Register the CXF Servlet.
 <!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>CXF Web Service Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/app-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Step 5: Building the Project
mvn clean install

Step 6: Deploying the Service
Now you can deploy this service on to any container. Here we'll deploy this on Tomcat server.

Step 7: Testing the RESTful Web Service using CURL

We're going to test our Web Service using CURL, which I found as a very easy way to send REST Calls. I found this very nice blog about RESTing with Curl.

Installing CURL

sudo apt-get install curl

-I am using Ubuntu, if you're using Win or Mac, please do the necessary to install curl.

Now, after successfully installing CURL, we are ready to test the services.

GET Resources:
- Retrieving all the employee resources.
 curl -v -H "Accept: application/json" http://localhost:8080/CXFEmployeeJsonRESTService/employeeservice/getall

- Retrieving a employee resource for a given id(id = 1).
 curl -v -H "Accept: application/json" http://localhost:8080/CXFEmployeeJsonRESTService/employeeservice/get/1

POST a Resource: Adding a new employee resource.
- Adding the new employee record in the add.json file.
 { 
  "ns1.Employee":{ 
    "id":1, 
    "firstName":"First Name1", 
    "lastName":"Last Name1" 
  } 
} 

 curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d @add.json http://127.0.0.1:8080/CXFEmployeeJsonRESTService/employeeservice/add


PUT a Resource: Updating an existing employee resource.
- Updating an existing employee record in the update.json file.
 { 
  "ns1.Employee":{ 
    "id":1, 
    "firstName":"Updated First Name1", 
    "lastName" :"Updated Last Name1" 
  } 
} 

 curl -v -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d @update.json http://127.0.0.1:8080/CXFEmployeeJsonRESTService/employeeservice/update/

DELETE a Resource: Deleting an employee for a given id (id=2)
 curl -i -H "Accept: application/json" -X DELETE http://localhost:8080/CXFEmployeeJsonRESTService/employeeservice/delete/2/

Summary:
This tutorial looked at how we can create JSON RESTful Web Service using Apache CXF and test the services using CURL. You can find the sample code here.

Part 1: RESTful WS with Apache CXF - XML

Overview:
This is Part 1 of the article on RESTful WS with Apache CXF. In this article we will look at how you can do XML/HTTP based RESTful WS with Apache CXF. We're using the CXF support on JAX-RS (JSR-311) for this. The sample code can be found here.

You can also follow the Part 2.
Prerequisite:JDK >= 1.5.x
Maven >= 2.2.x

Project Structure: Following is the project structure I have used (Maven WAR project).

Step 1: Maven Dependencies (pom.xml)
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fazlan</groupId>
<artifactId>org.fazlan.employee.cxf.xmlrest.service</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>org.fazlan.employee.cxf.xmlrest.service</name>
<url>http://maven.apache.org</url>
<build>
<finalName>CXFEmployeeXmlRESTService</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-aegis</artifactId>
<!-- 2.4.4 or 2.5.0 -->
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<properties>
<org.springframework.version>3.1.1.RELEASE</org.springframework.version>
<cxf.version>2.4.6</cxf.version>
</properties>
</project>

Step 2: Creating POJO Classes and Service Implementation

Employee.java
 package org.fazlan.employee.cxf.xmlrest.service.beans;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.io.Serializable;
@XmlType(
name = "EmployeeType",
namespace = "org.fazlan.employee.cxf.xmlrest.service.beans",
propOrder = {"id", "firstName", "lastName"})
@XmlRootElement(
name = "Employee",
namespace = "org.fazlan.employee.cxf.xmlrest.service.beans")
public class Employee implements Serializable {
private Long id;
private String firstName;
private String lastName;
public Employee() {
}
public Employee(Long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Employee)) return false;
Employee employee = (Employee) o;
return !(firstName != null ? !firstName.equals(employee.firstName) : employee.firstName != null) && !(id != null ? !id.equals(employee.id) : employee.id != null) && !(lastName != null ? !lastName.equals(employee.lastName) : employee.lastName != null);
}
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}

Employees.java - This is used to hold a collection object of Employee.java

 package org.fazlan.employee.cxf.xmlrest.service.beans;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.util.Collection;
@XmlType(
name = "EmployeesType",
namespace = "org.fazlan.employee.cxf.xmlrest.service.beans")
@XmlRootElement(
name = "Employees",
namespace = "org.fazlan.employee.cxf.xmlrest.service.beans")
public class Employees {
private Collection<Employee> employees;
public Employees() {
}
public Employees(Collection<Employee> employees) {
setEmployees(employees);
}
@XmlElement(name = "employee",required = true)
@XmlElementWrapper(name = "employees")
public Collection<Employee> getEmployees() {
return employees;
}
public void setEmployees(Collection<Employee> employees) {
this.employees = employees;
}
}
IEmployeeService.java - Service Interface

package org.fazlan.employee.cxf.xmlrest.service.services;
import org.fazlan.employee.cxf.xmlrest.service.beans.Employee;
import org.fazlan.employee.cxf.xmlrest.service.beans.Employees;
import javax.ws.rs.core.Response;
public interface IEmployeeService {
Employee get(Long id);
Employees getAll();
Response add(Employee employee);
Response update(Employee employee);
Response delete(Long id);
}
EmployeeService.java - Implementation of the service.

NOTE: This service class has been annotated to produce and consume certain methods in XML(application/xml) as the mime-type. Read more about Apache CXF - JAX-RS basics.

package org.fazlan.employee.cxf.xmlrest.service.services;
import org.fazlan.employee.cxf.xmlrest.service.beans.Employee;
import org.fazlan.employee.cxf.xmlrest.service.beans.Employees;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Map;
@Path("/employeeservice/")
@Produces("application/xml")
public class EmployeeService implements IEmployeeService {
private static final Map<Long, Employee> EMPLOYEE_MAP = new HashMap<Long, Employee>();
private static long index = 3L;
static {
EMPLOYEE_MAP.put(1L, new Employee(1L, "First Name 1", "Last Name 1"));
EMPLOYEE_MAP.put(2L, new Employee(2L, "First Name 2", "Last Name 2"));
}
@GET
@Path("/get/{id}")
public Employee get(@PathParam("id") Long id) {
return EMPLOYEE_MAP.get(id);
}
@GET
@Path("/getall/")
public Employees getAll() {
return new Employees(EMPLOYEE_MAP.values());
}
@POST
@Path("/add/")
@Consumes("application/xml")
public Response add(Employee employee) {
System.out.println("Adding :" + employee);
employee.setId(index++);
update(employee);
return Response.status(Response.Status.OK).build();
}
@PUT
@Path("/update/")
@Consumes("application/xml")
public Response update(Employee employee) {
EMPLOYEE_MAP.put(employee.getId(), employee);
return Response.status(Response.Status.OK).build();
}
@DELETE
@Path("/delete/{id}/")
public Response delete(@PathParam("id") Long id) {
Employee e = EMPLOYEE_MAP.remove(id);
System.out.println("Deleted :" + e);
return Response.status(Response.Status.OK).build();
}
}

Read more about Apache CXF - JAX-RS basics.

Step 3: Exposing the Service Beans as RESTful Web Service
 <?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:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://cxf.apache.org/jaxrs
     http://cxf.apache.org/schemas/jaxrs.xsd
     http://cxf.apache.org/core
     http://cxf.apache.org/schemas/core.xsd">
<!-- do not use import statements if CXFServlet init parameters link to this beans.xml -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxrs:server id="employeeService" address="/">
<jaxrs:serviceBeans>
<ref bean="employeeServiceBean"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</jaxrs:extensionMappings>
<jaxrs:features>
<cxf:logging/>
</jaxrs:features>
</jaxrs:server>
<bean id="employeeServiceBean" class="org.fazlan.employee.cxf.xmlrest.service.services.EmployeeService"/>
</beans>


Step 4: Updating the web.xml to Register the CXF Servlet.
 <!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>CXF Web Service Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/app-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Step 5: Building the Project
mvn clean install

Step 6: Deploying the Service
Now you can deploy this service on to any container. Here we'll deploy this on Tomcat server.

Step 7: Testing the RESTful Web Service using CURL

We're going to test our Web Service using CURL, which I found as a very easy way to send REST Calls. I found this very nice blog about RESTing with Curl.

Installing CURL

sudo apt-get install curl

-I am using Ubuntu, if you're using Win or Mac, please do the necessary to install curl.

Now, after successfully installing CURL, we are ready to test the services.

GET Resources:
- Retrieving all the employee resources.
 curl -v -H "Accept: application/xml" http://localhost:8080/CXFEmployeeXmlRESTService/employeeservice/getall

- Retrieving a employee resource for a given id(id = 1).
 curl -v -H "Accept: application/xml" http://localhost:8080/CXFEmployeeXmlRESTService/employeeservice/get/1

POST a Resource: Adding a new employee resource.
- Adding the new employee record in the add.xml file.
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Employee xmlns:ns2="org.fazlan.employee.cxf.xmlrest.service.beans">
<id>3</id>
<firstName>First Name1</firstName>
<lastName>Last Name1</lastName>
</ns2:Employee>

 curl -v -H "Accept: application/xml" -H "Content-type: application/xml" -X POST -d @add.xml http://127.0.0.1:8080/CXFEmployeeXmlRESTService/employeeservice/add


PUT a Resource: Updating an existing employee resource.
- Updating an existing employee record in the update.xml file.
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Employee xmlns:ns2="org.fazlan.employee.cxf.xmlrest.service.beans">
<id>1</id>
<firstName>Updated First Name1</firstName>
<lastName>Updated Last Name1</lastName>
</ns2:Employee>

 curl -v -H "Accept: application/xml" -H "Content-type: application/xml" -X PUT -d @update.xml http://127.0.0.1:8080/CXFEmployeeXmlRESTService/employeeservice/update/

DELETE a Resource: Deleting an employee for a given id (id=2)
 curl -i -H "Accept: application/xml" -X DELETE http://localhost:8080/CXFEmployeeXmlRESTService/employeeservice/delete/2/

Summary:
This tutorial looked at how we can create XML RESTful Web Service using Apache CXF and test the services using CURL. You can find the sample code here.

Wednesday, April 11, 2012

Web Service: Apache CXF Web Service Tutorial

Overview:
This article looks at how to create a Web Service by using Apache CXF. Apache CXF treats Spring as its first class citizen and enables exposing of existing Spring beans as Web Services. You can find the sample code here.

Project Structure: Following is the project structure I have used (Maven WAR project).


Step 1: Maven Dependencies (pom.xml)
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fazlan</groupId>
<artifactId>org.fazlan.employee.cxfspring.service</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>org.fazlan.employee.cxfspring.service</name>
<url>http://maven.apache.org</url>
<build>
<finalName>CXFEmployeeService</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
<properties>
<org.springframework.version>3.1.1.RELEASE</org.springframework.version>
<cxf.version>2.2.3</cxf.version>
</properties>
</project>

Step 2: Creating POJO Classes and Service Implementation
Employee.java
 @XmlType(name = "Employee", namespace = "org.fazlan.employee.cxfspring.service.beans")
public class Employee implements Serializable {
private Long id;
private String firstName;
private String lastName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

Service Interface and Implementation

IEmployeeService.java
 import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(targetNamespace = "org.fazlan.employee.cxfspring.service.services")
public interface IEmployeeService {
@WebMethod(operationName = "AddEmployee")
@WebResult(name = "id")
Long add(@WebParam(name = "employee") Employee employee);
@WebMethod(operationName = "UpdateEmployee")
void update(@WebParam(name = "employee") Employee employee);
@WebMethod(operationName = "GetEmployee")
@WebResult(name = "employee")
Employee get(@WebParam(name = "id") Long id);
@WebMethod(operationName = "GetAllEmployees")
@WebResult(name = "employee")
Employee[] getAll();
}

EmployeeService.java
 import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;
@WebService(
endpointInterface = "org.fazlan.employee.cxfspring.service.services.IEmployeeService",
targetNamespace = "org.fazlan.employee.cxfspring.service.services",
serviceName = "EmployeeService")
public class EmployeeService implements IEmployeeService {
private static final Map<Long,Employee> EMPLOYEE_MAP = new HashMap<Long,Employee>();
private static long index = 0;
public Long add(Employee employee) {
employee.setId(++index);
update(employee);
return index;
}
public void update(Employee employee) {
EMPLOYEE_MAP.put(employee.getId(), employee);
}
public Employee get(Long id) {
return EMPLOYEE_MAP.get(id);
}
public Employee[] getAll() {
return EMPLOYEE_MAP.values().toArray(new Employee[EMPLOYEE_MAP.size()]);
}
}

Step 3: Exposing the service bean as a Web Service
app-context.xml
 <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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint
id="employeeService"
implementor="org.fazlan.employee.cxfspring.service.services.EmployeeService"
address="/employeeservice"/>
</beans>

Step 4: Updating the web.xml to register CXFServlet

 <!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>CXF Web Service Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/app-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Step 5: Building the project
mvn clean install

Step 6: Deploying the Service
Now you can deploy this service on to any container. Here we'll deploy this on Tomcat server.
After successfully deploying the service, you can access the service via
http://localhost:8080/CXFEmployeeService/employeeservice?wsdl

Step 7: Invoking the Services via SOAP UI.

You can easily test the services using SOAP UI. Use the service URL(http://localhost:8080/CXFEmployeeService/employeeservice?wsdl) and create a project on SOAP UI.


Summary:
Apache CXF is a Web Service Engine that enables us to deploy Spring beans as Web Services on any Web Container.