Building a JSON web service with Java and Axis2

It is said that in Java usually the easiest things are the hardest. The following is a nice example as it took me some time to figure out how to do it.
I wanted to build a simple URL request based web service in Java that returns an JSON object. Yes, you can do that with a simple Servlet too, the advantage of using Axis2 is that you can also call your deployed services using SOAP without any configuration changes.

  1. Download Axis2 as WAR and install it in your servlet container
  2. Download the DynamicResponseHandler module and add it to Axis by copying it to WEB-INF/modules
  3. Patch Jettison or download my patched version and replace it with the one installed in WEB-INF/lib
  4. Add the DynamicResponseHandler module reference to the axis2.xml configuration (located in WEB-INF/conf):
  5. Add the JSON Message formatters to the axis2.xml:
  6. Add JSON Message builders to the axis2.xml:
  7. Start your servlet container and test the standard version service by calling this url:
    http://localhost:8080/axis2/services/Version/getVersion?response=application/json

Now you are ready to add your own web services. Here you can find an example how to deploy a simple POJO service. Have fun!

Update: Zeno (see comments) sent me patch for usage with Jettison 1.2 – otherwise he received a NullPointerException. I haven’t checked it, but I hope it helps you! Thanks Zeno!

  • Kedar

    Marcus, this is what I was looking for. I will try this out. However I would like to know whether Axis 2 supports generating the JSON Client jars?

  • Marcus Schiesser

    Kedar, I don’t completly understand your question, what JSON client jars?

  • srikanth

    This is a avery good piece of information. Could you also tell us how the java client code can recieve this JSON output?

  • Marcus Schiesser

    sorry, I was using this only with a javascript client. Hopefully someone else can help.

  • Dhanasekar

    Hi,

    Thanks for the blog.

    Can you please elobarate step ‘Add the DynamicResponseHandler module reference to the axis2.xml configuration (located in WEB-INF/conf): ‘

    Is it only thing i need to add to refer the module in axis 2?

  • Pingback: Meeting of Minds » Blog Archive » Building a JSON Axis2 REST Client for a WCF Service.()

  • Anurag

    Does the WS support hashtable or hashmap. I am not able to send response with these 2 datatypes

  • Zeno

    Hi Marcus, thanks for this work, is very useful.

    I wrote you an e-mail with a possibly update for this page with Jettison 1.2 patched and another clean solution that I found on the web.

  • Mahesh kumar

    Hi Marcus, I’m uning axis2-1.6. I modified the axis2.xml in conf folder shipped with axis2 binary. Added module. Added jar. In other words I followed the steps above. But still I’m getting response in xml format. Could you assist me?

  • Ramesh

    Hi Mahesh,

    If you found the solution, could you pass it on to me and also if possible could send me sample wsdl file. Its very urgent for me.
    Thanks in advance for your help.

    Thanks,

  • Pingback: JSON web service with Java and Axis2 at A.K.A Self notes()

  • Pingback: RedEyeWatching Blog - JSON Provider erstellen. Part 3: Axis2 spricht JSON()

  • Dieter

    Hi Marcus,
    Some output as Mahesh Kumar.
    => No JSON, but still XML…

  • seki

    Dear Marcus Schiesser,

    i’m a javascript developer from china.
    i’ve read your article “Building a JSON web service with Java and Axis2”
    (/2009/01/building-a-json-web-service-with-java-and-axis2/)
    and it’s so helpful for me to resolve the problem with axis2 and json.

    use the way you told in the article , i can get a json string directly from a webserive url like
    “http://xxxx/xxxx?response=json”, but the string i got from the url begins with something like

    “{“return”:[{“@type”:”ax21:CategoryInfoBean”,”categoryId”:”ecology”,”categoryName”:”

    as the root element “return” is a keyword in javascript, it’s not so easy to get the data from it.
    my question is, is there anyway to change the root element name from “return” to something
    else?

    i’ve tried to look for it in the jettison’s source code and couldn’t find anything useful.

    it will be very kind of you to give me some advice.

    thank you very much.

  • Is SSL required or can be without it all work?

    Thanks!

  • Mandeep

    Hi
    json object returned by axis web service dont work in cross domain while getting it in jquery.getjson.Do have any idea , how to get it work?

  • duffman

    i’ve got the same question as seki.
    is there any way to get rid of the “return”?
    did anybody solve it?

  • Datchayani

    I am getting error this error: how to fix this error {“Fault”:”soapenv:ReceiverMapped formatted JSON with namespaces are not supported in Axis2. Make sure that your request doesn’t include namespaces or use the Badgerfish convention”}

  • Slim

    Hi Marcus, can u give me an exemple to call web
    service about wsdl!!!
    Thank u

  • sarfaraz

    Sir can provide the complete code so that I can understand it more clearly..I want both from client side and server side code….or any tutorial which has complete code in it…thank uuuuuuuuu

  • allopez

    Hello I just get a JSON from Axis2 Version Service.
    From a Hello World example I just get an XML result formatted from a string.
    Any news to solve this problem?

    Danke.

  • OM

    When i try to use our example i have this:
    java.lang.NullPointerException
    at org.codehaus.jettison.mapped.MappedXMLStreamWriter.writeEndElement(MappedXMLStreamWriter.java:247)
    at org.apache.axiom.om.impl.MTOMXMLStreamWriter.writeEndElement(MTOMXMLStreamWriter.java:171)
    at org.apache.axiom.om.impl.util.OMSerializerUtil.serializeEndpart(OMSerializerUtil.java:57)
    at org.apache.axiom.om.impl.llom.OMElementImpl.internalSerialize(OMElementImpl.java:973)
    at org.apache.axiom.om.impl.llom.OMSerializableImpl.serialize(OMSerializableImpl.java:125)
    at org.apache.axiom.om.impl.llom.OMSerializableImpl.serializeAndConsume(OMSerializableImpl.java:117)
    at org.apache.axis2.json.JSONMessageFormatter.writeTo(JSONMessageFormatter.java:181)

    Any idea