tumblr counter

CXF Web Service Tutorial

CXF Web Service Tutorial explains about step by step details of Creating / Developing Web service using Apache CXF, Spring & Eclipse and deployed in Tomcat

Apache CXF is a free and open source project, and a fully featured Webservice framework.

It helps you building webservices using different front-end API's, like as JAX-RS and JAX-WS.

Services will talk different protocols such as SOAP, RESTful HTTP, CORBA & XML/HTTP and work with different transports like JMS, HTTP or JBI.

Apache CXF Project was created by the merger of the Celtix and XFire projects. These two projects were merged by folks working together at the Apache Software Foundation.

Note

You can also read  about implementing an Asynchronous service Asynchronous Web Service Using CXF

Required Libraries

You need to download

  1. JDK 6
  2. Eclipse 3.7
  3. CXF-2.7.3
  4. Tomcat 7

Following jar must be in classpath

  1. commons-logging-1.1.1.jar
  2. aopalliance-1.0.jar
  3. cxf-2.7.3.jar
  4. httpasyncclient-4.0-beta3.jar
  5. httpclient-4.2.1.jar
  6. httpcore-4.2.2.jar
  7. httpcore-nio-4.2.2.jar
  8. neethi-3.0.2.jar
  9. spring-aop-3.0.7.RELEASE.jar
  10. spring-asm-3.0.7.RELEASE.jar
  11. spring-beans-3.0.7.RELEASE.jar
  12. spring-context-3.0.7.RELEASE.jar
  13. spring-core-3.0.7.RELEASE.jar
  14. spring-expression-3.0.7.RELEASE.jar
  15. spring-web-3.0.7.RELEASE.jar
  16. wsdl4j-1.6.2.jar
  17. xmlschema-core-2.0.3.jar

CXF Tutorial

I am creating a sample web service project that pass Student object and return with some changes on that object. The service is using simple POJO (Plain Old Java Object) bean.

Firstly create a Dynamic Web Project (File->New->Dynamic Web Project) named "CXFTutorial" according to following screenshot

Create CXF Project CXF Tutorial

Create a Student Object

package com.student;

public class Student {
 
private String name;
 
public String getName() {
   
return name;
 
}
 
public void setName(String name) {
   
this.name = name;
 
}
}

Create a Service Interface

This service interface will defines which methods of web service, to be invoked by the client

package com.student;

import javax.jws.WebService;

@WebService
public interface ChangeStudentDetails {
 
Student changeName(Student student);
}

Implement the Service Interface

Here we implement the service interface created on the previous step

package com.student;

import javax.jws.WebService;

@WebService(endpointInterface = "com.student.ChangeStudentDetails")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {
   
public Student changeName(Student student) {
     
student.setName("Hello "+student.getName());
     
return student;
   
}
}

Create a cxf.xml

CXF is using Spring internally, Finding classes by spring we need to add service implementation class on "jaxws:endpoint" tag

<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">

    <jaxws:endpoint id="changeStudent"
        implementor="com.student.ChangeStudentDetailsImpl" address="/ChangeStudent" />

</beans>

#pager#

Change web.xml

Change the web.xml file to find CXF servlet and cxf.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/cxf.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>

Publishing CXF Web Service

Run CXF On Tomcat

Note

You can Find all the deployed JAX-WS/JAX-RS services you need to append 'services' at the end of the URL so URL will become following

http://localhost:8080/CXFTutorial/services

Deployed CXF Web Service

CXF WebService Running

Note

you can also see CXF client example in order to run this service







56 Responses to “CXF Web Service Tutorial”
  1. Srajan Dongre 22.06.2012 18:45:14
  1. admin 24.06.2012 18:45:48
  1. Sharath 28.06.2012 18:46:39
  1. Liushan_33 06.07.2012 18:47:17
  1. admin 07.07.2012 17:37:29
  1. Suresh Shetty 10.07.2012 18:49:16
  1. Sriharshakothuru 17.07.2012 18:54:52
  1. vsmak 22.07.2012 18:55:42
  1. admin 23.07.2012 17:43:11
  1. Amar 08.08.2012 19:02:06
  1. admin 10.08.2012 17:41:45
  1. Java 13.02.2013 11:41:59
  1. Java 13.02.2013 14:27:44
  1. admin 17.02.2013 00:26:08
  1. Julien 10.03.2013 11:40:11
  1. Julien 11.03.2013 06:23:54
  1. Murali 12.03.2013 03:10:35
  1. mayur 22.03.2013 03:28:21
  1. admin 27.03.2013 09:51:09
  1. Rohan Kulkarni 28.03.2013 04:02:19
  1. admin 29.03.2013 11:37:48
  1. JW 09.04.2013 17:25:21
  1. admin 10.04.2013 12:24:44
  1. freeman 17.04.2013 21:49:54
  1. freeman 17.04.2013 22:12:57
  1. admin 19.04.2013 13:56:13
  1. chandu 20.04.2013 02:33:15
  1. admin 21.04.2013 13:00:41
  1. Pasha 22.04.2013 11:01:47
  1. techy 21.05.2013 03:12:46
  1. admin 23.05.2013 10:22:51
  1. keith 19.06.2013 02:25:36
  1. Ravi 16.09.2013 09:42:58
  1. admin 16.09.2013 11:16:48
  1. pnp 07.10.2013 05:32:48
  1. admin 07.10.2013 11:14:20
  1. Praveen D 28.10.2013 12:50:35
  1. admin 09.11.2013 03:39:33
  1. sanjeev 20.11.2013 09:41:58
  1. admin 20.11.2013 22:16:12
  1. sanjeev 21.11.2013 08:33:24
  1. anirudh 24.04.2014 11:43:16
  1. Aarif Choudhary 19.05.2014 17:56:17
  1. admin 20.05.2014 07:57:00
  1. saran 22.05.2014 12:44:49
  1. dola kec 31.05.2014 22:24:28
  1. admin 20.06.2014 10:09:40
  1. Satish Ch 29.07.2014 21:33:17
  1. admin 31.07.2014 07:43:43
  1. raj 24.09.2014 18:10:58
  1. admin 24.09.2014 19:56:19
  1. Nitesh 10.12.2014 10:36:38
  1. barath 20.12.2014 00:48:17
  1. admin 06.01.2015 19:46:47
  1. marina 19.02.2015 23:16:31
  1. admin 21.02.2015 08:38:22

Your email address will not be published. Required fields are marked *