Sunday, October 23, 2011

Using Jackson with Apache CXF - JAX-RS

Apache CXF uses Jettison for generating JSON generation. By default, Jettison adds the class name as well in JSON representation. To remove the class name from the json, there is some extra configuration required, which I will tell you some other time. Another solution to this problem is to use Jackson.

public class Error{

   Integer code;
   String reason;

   /*
    * getters and setters 
    */
   
}

Jettison's JSON:



{ "error" : { code: "CODE", reason: "REASON } }

Jackson's JSON:

{ code: "CODE", reason: "REASON" }




Now I will explain how to tell CXF to use Jackson instead of Jettison. Following are the required steps:

  1. You need to add Jackson in your classpath. If you are using Maven, you just need to add following lines in your pom.xml:
  2.       
             org.codehaus.jackson
             jackson-jaxrs
             1.9.0
          
    
  3. Next thing is to tell CXF to use Jackson. If you using Spring, you can add following lines in your bean configuration xml:
  4.