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 integrating CXF with JBoss Application Server CXF With JBoss Tutorial

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

Required Libraries

You need to download

  1. OpenJDK 8 / Corretto
  2. Eclipse 4.15
  3. CXF-3.3.6
  4. Tomcat 9

Following jar must be in classpath

  1. cxf-core-3.3.6.jar
  2. cxf-rt-bindings-soap-3.3.6.jar
  3. cxf-rt-databinding-jaxb-3.3.6.jar
  4. cxf-rt-features-logging-3.3.6.jar
  5. cxf-rt-frontend-jaxws-3.3.6.jar
  6. cxf-rt-frontend-simple-3.3.6.jar
  7. cxf-rt-transports-http-3.3.6.jar
  8. cxf-rt-wsdl-3.3.6.jar
  9. httpasyncclient-4.1.4.jar
  10. httpclient-4.5.12.jar
  11. httpcore-4.4.13.jar
  12. httpcore-nio-4.4.13.jar
  13. neethi-3.1.1.jar
  14. slf4j-api-1.7.29.jar
  15. slf4j-jdk14-1.7.29.jar
  16. spring-aop-5.1.14.RELEASE.jar
  17. spring-beans-5.1.14.RELEASE.jar
  18. spring-context-5.1.14.RELEASE.jar
  19. spring-core-5.1.14.RELEASE.jar
  20. spring-expression-5.1.14.RELEASE.jar
  21. spring-jcl-5.1.14.RELEASE.jar
  22. spring-web-5.1.14.RELEASE.jar
  23. stax2-api-3.1.4.jar
  24. woodstox-core-5.0.3.jar
  25. wsdl4j-1.6.3.jar
  26. xmlschema-core-2.2.5.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>

Change web.xml

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

<!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>Archetype Created Web Application</display-name>
  <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









61 Responses to "CXF Web Service Tutorial"
  1. vsmak 2011-09-22 21:50:29.0
  1. admin 2011-09-23 01:50:29.0
  1. Srajan Dongre 2011-09-23 21:50:29.0
  1. admin 2011-09-23 22:50:29.0
  1. Sharath 2011-09-24 21:50:29.0
  1. Liushan_33 2011-09-25 00:50:29.0
  1. admin 2011-09-25 01:50:29.0
  1. Suresh Shetty 2011-09-25 21:50:29.0
  1. Sriharshakothuru 2011-09-25 22:50:29.0
  1. Amar 2011-09-26 16:50:19.0
  1. admin 2011-09-27 16:50:19.0
  1. Java 2011-09-28 17:50:29.0
  1. Java 2011-09-29 17:50:29.0
  1. admin 2011-09-30 17:50:29.0
  1. Julien 2011-10-01 17:50:29.0
  1. Julien 2011-10-02 17:50:29.0
  1. Murali 2011-10-03 17:50:29.0
  1. mayur 2011-10-04 17:50:24.0
  1. admin 2011-10-05 17:50:24.0
  1. Rohan Kulkarni 2011-10-06 17:50:14.0
  1. admin 2011-10-07 17:50:14.0
  1. JW 2011-10-08 17:50:14.0
  1. admin 2011-10-09 17:50:14.0
  1. freeman 2011-10-10 17:50:14.0
  1. freeman 2011-10-11 17:50:14.0
  1. admin 2011-10-12 17:50:14.0
  1. chandu 2011-10-13 17:50:14.0
  1. admin 2011-10-14 17:50:14.0
  1. Pasha 2011-10-15 17:50:14.0
  1. techy 2011-10-16 17:50:41.0
  1. admin 2011-10-17 17:50:41.0
  1. keith 2011-10-18 17:50:41.0
  1. Ravi 2011-10-19 17:50:41.0
  1. admin 2011-10-22 17:50:41.0
  1. pnp 2011-10-25 17:50:41.0
  1. admin 2011-10-26 17:50:41.0
  1. Praveen D 2011-10-27 17:50:41.0
  1. admin 2011-10-28 17:50:41.0
  1. sanjeev 2011-10-29 17:50:41.0
  1. admin 2011-10-30 16:50:41.0
  1. sanjeev 2011-11-01 16:50:41.0
  1. anirudh 2011-11-02 16:50:41.0
  1. Aarif Choudhary 2011-11-03 16:50:41.0
  1. admin 2011-11-04 16:50:41.0
  1. saran 2011-11-05 16:50:41.0
  1. dola kec 2011-11-06 16:50:32.0
  1. admin 2011-11-07 16:50:32.0
  1. Ch 2011-11-08 16:50:32.0
  1. admin 2011-11-09 16:50:32.0
  1. raj 2011-11-10 16:50:32.0
  1. admin 2011-11-11 16:50:32.0
  1. Nitesh 2011-11-12 16:50:32.0
  1. barath 2011-11-13 16:50:32.0
  1. admin 2011-11-14 16:50:32.0
  1. marina 2011-11-15 16:50:32.0
  1. admin 2011-11-16 16:50:32.0
  1. wendell 2011-11-17 16:50:32.0
  1. admin 2011-11-18 16:50:32.0
  1. Vinoth 2011-11-19 16:50:32.0
  1. krishna 2011-11-20 16:50:32.0
  1. David 2017-11-24 06:30:07.0

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