Apache CXF With Jackson

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 implement 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;

public class ChangeStudentDetailsImpl implements ChangeStudentDetails {

public Student changeName(Student student) {
student.setName("HELLO " + student.getName());
return student;

public Student getName() {
Student student = new Student();
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".

We also created a bean

<bean class="com.student.CustomJsonProvider" id="jacksonJsonProvider" /> in order to use jackson because CXF is default using jettison

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation=" http://www.springframework.org/schema/beans 
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> 
<!--Here we are adding jacksonJsonProvider -->
	<bean id="jacksonJsonProvider" class="com.student.CustomJsonProvider" />
	<jaxrs:server id="base" address="/rest">
			<ref bean="StudentService" />
			<ref bean="jacksonJsonProvider" />
	<bean id="StudentService" class="com.student.ChangeStudentDetailsImpl" />

Implement JacksonJaxbJsonProvider

Here we are implementing JacksonJaxbJsonProvider and mentioned this on cxf.xml

package com.student;
import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;

public class CustomJsonProvider extends JacksonJaxbJsonProvider {

public CustomJsonProvider(){

ObjectMapper mapper = new ObjectMapper();
(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
().setAnnotationIntrospector(new JaxbAnnotationIntrospector());


Previous 1 2 3 Next

5 Responses to "Apache CXF With Jackson"
  1. Suraj 2012-07-05 07:53:15.0
  1. admin 2012-07-06 07:53:15.0
  1. sochs 2012-07-07 07:53:15.0
  1. Nitin 2012-07-08 08:53:15.0
  1. admin 2012-07-09 07:53:15.0

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