tumblr counter

CXF Restful Tutorial

CXF Restful Tutorial explains step by step details of Creating / Developing Java rest Web services using Apache CXF, Spring and Eclipse

JAX-RS is Java API for RESTful Webservices which is very rely upon Representational State Transfer model, you can view JAX-RS specification 

JAX-RS uses annotations for simplifying the development efforts.

Now a days more & more deployment is going based on restful services compare to WSDL Webservices, due to the weights towards the simplicity of configuration

You can see the below example, which is demonstrating How to create a Restful service using CXF

Note

You can also find tutorial about CXF With Jackson

Required Libraries

You need to download

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

Following jar must be in classpath

  1. commons-logging-1.1.1.jar
  2. cxf-2.7.3.jar
  3. httpasyncclient-4.0-beta3.jar
  4. httpclient-4.2.1.jar
  5. httpcore-4.2.2.jar
  6. httpcore-nio-4.2.2.jar
  7. neethi-3.0.2.jar
  8. spring-aop-3.0.7.RELEASE.jar
  9. spring-asm-3.0.7.RELEASE.jar
  10. spring-beans-3.0.7.RELEASE.jar
  11. spring-context-3.0.7.RELEASE.jar
  12. spring-core-3.0.7.RELEASE.jar
  13. spring-expression-3.0.7.RELEASE.jar
  14. spring-web-3.0.7.RELEASE.jar
  15. wsdl4j-1.6.2.jar
  16. jaxb-impl-2.2.6.jar
  17. javax.ws.rs-api-2.0-m10.jar
  18. xmlschema-core-2.0.3.jar
  19. jettison-1.3.3.jar (JSON library)

CXF Restful Tutorial

I am creating a sample restful 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 "CXFRestfulTutorial" according to following screenshot

Create CXF Project CXF Restful Tutorial

Create a Student Object

package com.student;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Student")
public class Student {
 
private String name;

 
public String getName() {
   
return name;
 
}

 
public void setName(String name) {
   
this.name = name;
 
}

}
Here @XmlRootElement(name = "Student"), is a JAXB convension specifies that Student is XML document.

If you are specifies that @Produces("application/json") then Jettison library converts the JAXB to json text as response

Create a Service Interface

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

package com.student;


public interface ChangeStudentDetails {
 
Student changeName(Student student);
  Student getName
();
}

Implement the Service Interface

Here we implment the service interface created on the previous step

Here we are using one example showing with GET method& another with POST method

GET---> Calling this method will not result any changes to the server

POST---> Calling this method will result changes to the server, This have more secure than GET method

package com.student;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Consumes("application/json")
@Produces("application/json")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {

 
@POST
  @Path
("/changeName")
 
public Student changeName(Student student) {
   
student.setName("HELLO " + student.getName());
   
return student;
 
}

 
@GET
  @Path
("/getName")
 
public Student getName() {
   
Student student = new Student();
    student.setName
("Rockey");
   
return student;
 
}
}

Note; On the above ChangeStudentDetailsImpl class, implementing an interface is not necessity, you can create restful services without implementing an interface.

@Consumes annotation specifies, the request is coming from the client

you can specify the Mime type as @Consumes("application/xml"), if the request is in xml format

@Produces annotation specifies, the response is going to the client

you can specify the Mime type as @Produces ("application/xml"), if the response need to be in xml format

Create a cxf.xml

CXF is using Spring internally, Finding classes by spring we need to add service implementation beans are added on "jaxrs:serviceBeans".

<?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"
    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">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <jaxrs:server id="base" address="/rest">
        <jaxrs:serviceBeans>
            <ref bean="StudentService" />
        </jaxrs:serviceBeans>
    </jaxrs:server>
    <bean id="StudentService" class="com.student.ChangeStudentDetailsImpl" />
</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 version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <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 Restful Service

Run CXF Restful 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/CXFRestfulTutorial/services

Deployed REST Web Service Using CXF

CXF Restful Service Running

Note

you can also see CXF Restful Client in order to run this restful service








25 Responses to “CXF Restful Tutorial”
  1. Yevgeny Vasilyev 04.09.2012 18:37:18
  1. admin 10.09.2012 18:36:35
  1. pep 12.11.2012 17:22:37
  1. admin 14.11.2012 17:21:19
  1. sony 10.03.2013 03:53:53
  1. Julien 11.03.2013 10:53:45
  1. admin 27.03.2013 09:59:14
  1. Ruchika 08.04.2013 15:58:52
  1. admin 09.04.2013 23:44:45
  1. steve 17.04.2013 05:38:04
  1. steve chan 17.04.2013 21:27:21
  1. admin 19.04.2013 13:49:01
  1. Madusha 05.06.2013 13:43:22
  1. admin 06.06.2013 00:12:32
  1. Deepak 26.07.2013 01:31:14
  1. admin 26.07.2013 11:42:29
  1. bibhu 28.08.2013 07:38:38
  1. admin 02.02.2014 22:38:22
  1. Ram 24.09.2014 17:56:57
  1. admin 24.09.2014 19:47:03
  1. madhu 29.10.2014 20:44:51
  1. admin 30.10.2014 08:28:08
  1. madhu 30.10.2014 15:10:19
  1. admin 31.10.2014 11:09:04
  1. madhu 31.10.2014 16:09:29

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