Thursday, October 6, 2011

Enabling JSON in Apache Axis2


Apache Axis2 can handle several message formats. JSON is just one of them. Axis2 supports two notations of JSON, they are known as mapped and Badgerfish. Namespaces are not handled in the mapped notation while the Badgerfish notations supports it.

The default Axis2 installation has all the binaries needed to process JSON messages but you need to enable it in order to use JSON. This can be done by adding the following sections to your axis2.xml.

Add the following section to the messageFormatters section in the axis2.xml,
  1. <messageFormatter contentType="application/json"  
  2.   
  3.                  class="org.apache.axis2.json.JSONMessageFormatter"/>  
  4.   
  5. <messageFormatter contentType="application/json/badgerfish"  
  6.   
  7.                  class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>  
  8.   
  9. <messageFormatter contentType="text/javascript"  
  10.   
  11.                  class="org.apache.axis2.json.JSONMessageFormatter"/>  

And the following to the messageBuilders section of the axis2.xml
  1. <messageBuilder contentType="application/json"  
  2.   
  3.                  class="org.apache.axis2.json.JSONOMBuilder"/>  
  4.   
  5. <messageBuilder contentType="application/json/badgerfish"  
  6.   
  7.                  class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>  
  8.   
  9. <messageBuilder contentType="text/javascript"  
  10.   
  11.                  class="org.apache.axis2.json.JSONOMBuilder"/>  

Now you are all set to invoke services as well as respond using JSON. You would notice that the notation of JSON used depends on the content-type of the message.

2 comments:

  1. Hello, thank you for the post.

    I dont understand how to invoke my service for to respond in json. For example, I call the service in the URL: http://localhost:8080/ws/services/people/hello, but always return xml objects.

    Why?

    ReplyDelete
  2. you need to add content type as following:

    http://localhost:8080/ws/services/people/hello?content=application/json

    ReplyDelete