A.K.A Self notes

Online notes by and for Djaka PM

JSON web service with Java and Axis2

with 11 comments

I have recently met a client who asking me to rebuild their legacy product using Java web service.
They want it modular and easy to use. The first thing crossed my mind is using restful approach.

But there is only thing that bothers me, Java restful approach is using XML!, I would prefer a simpler way to communicate, easy to understand and parsed data, so Json it is. I started google-ing a little and came across this excellent blog.

I read the blog but it seem there some minor problem which they (he and the blog commenter) already solved.
So I am not here to revise on anything just to sum it up so it will be easier for you to build a Json web service with Java and Axis2.

I am using Java 1.7 and Apache Tomcat 7.0.

The steps are not so different with my reference blog, but I have minor modification on the steps.

1. Download the Axis2 war that includes jetisson patch and DynamicRespondHandler Axis2 Module here
Note that the Axis2 above is pre-configure,but still I would like to show what the changes are:

- Added module reference to the axis2.xml file

<module ref="DynamicResponseHandler"/>

- Added Json Message formatters

<messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONMessageFormatter"/>

<messageFormatter contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>

- Added Json Message builders

<messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONOMBuilder"/>

<messageBuilder contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>

2. Download you favorite Java application server, in this case I am using Apache Tomcat 7.0.

3. Deploy Axis2 war to your application server, in my case I just copy the Axis2 war to Tomcat`s webapps folder.

4. Start your application server and open this URL using your web browser (your URL might slightly different then mine, depend on your HTTP port setting on your application server):

http://localhost:8080/axis2/services/Version/getVersion?response=application/json

your browser should shows this:

Axis2 web service result

So that is the start, I hopefully can post more about this in the future. Cheers.

Written by djakapm

December 15th, 2011 at 4:25 pm

11 Responses to 'JSON web service with Java and Axis2'

Subscribe to comments with RSS or TrackBack to 'JSON web service with Java and Axis2'.

  1. Hi there,

    Nice blog! Is there an email address I can contact you in private?

    Ilias Tsagklis

    23 Dec 11 at 01:53

  2. Sure, you can contact me at jakenenator[at]gmail.com

    djakapm

    3 Jan 12 at 04:05

  3. Hi,

    I didn’t download the axis2.war file on your link but I followed the instructions you provided. Basically, I downloaded DynamicRespondHandler and put it inside the modules directory. Then I added a module reference to it at Axis2.xml. I also added the necessary message formatters and builders at axis2.xml. I also downloaded jettison-1.3.1.jar and put it inside the WEB-INF/lib directory.

    However, when I try to call my Axis2 web service with “response=application/json” parameter, I am getting an “org.apache.axis2.AxisFault: Mapped formatted JSON with namespaces are not supported in Axis2. Make sure that your request doesn’t include namespaces or use the Badgerfish convention” error message.

    On the other hand, I have no problem calling my webservice if the value of the response is “application/json/badgerfish”.

    Do you have any idea why am I encountering the error above? I call my web service using http GET: (http://localhost:8080/Dummy/services/Dummy/method?param1=dasda&response=application/json)

    Hannah

    3 Jan 12 at 10:44

  4. The old axis release did not include the fixes that involve the jettison-1.3.1.
    The problem actually resides in the jettison release.

    I suggested that you use the axis2.war on the blog, I think it will save your time a bit :D.

    djakapm

    25 Jan 12 at 13:42

  5. Hi
    I set Axis2 as a stand alone system and I can get the Server respond appropriately to my requests on 3 browsers – IE v8, Firefox 10.1.1 and Chrome.
    The below URL –
    http://localhost/axis2/services/Version/getVersion?response=text/javascript
    gives me a response as below -
    Hi – the Axis2 version is 1.6.1
    The below URL -
    http://localhost/axis2/services/Version?wsdl
    response is as -
    This XML file does not appear to have any style information associated with it. The document tree is shown below.

    Version

    As you can see that the suggested steps did not work for me. Obviously, I am missing something and I spent something like 1 full day on this trying to figure out, but invain.

    I have tried both the below settings -

    <messageFormatter contentType="text/javascript"

    Any help is greatly appreciated.

    Thanks

    Subrahmanyam

    20 Feb 12 at 22:32

  6. Hi Subrahmanyam,
    Thank you for the question.
    I am not sure that the mime type “text/javascript” would work because the its not a data format
    some of the data format I known are “text/xml” and/or “application/json”.
    I am sorry but your xml did not appear on your comment.
    What message formatter class do you use use?
    Its just a hunch, but could you try “application/javascript”?
    Could you give more information?
    I hope it helped.

    P.S The line below is harmless I think:
    “This XML file does not appear to have any style information associated with it”
    you can ignore it.

    djakapm

    21 Feb 12 at 20:24

  7. Hi Hannah,

    were you able to resolve the issue, I also tried the same but unable to do so for Jason.

    I am using
    Axis2 version is 1.6.2 (axis2.war) along with jettison-1.3.1.jar

    {“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”}

    Can you please guide me on this.

    thanks,
    Chandra Shekhar

    chandra shekhar

    21 May 12 at 13:10

  8. I also encountered the same situation, would you already have a solution to this problem?

    H.Q.

    3 Jul 12 at 10:47

  9. sir can u provide the complete code for better understanding or a tutorial which is already present..

    sarfaraz

    13 Sep 12 at 13:13

  10. JSON web service with Java and Axis2 | A.K.A Self notes…

    Thank you for submitting this cool story – Trackback from JavaPins…

    JavaPins

    16 Sep 12 at 10:49

  11. My web service
    public String pos(){
    System.out.println(“post”);
    return “{‘vbak_id’:’1000136′}”;
    }

    when run in browser

    {‘vbak_id’:’1000136′}

    call from jquery
    $.ajax({
    type: “POST”,
    url: url,
    data: data,
    dataType: “xml”,

    traditional: true,
    //crossDomain: true,
    //rootProperty:’results’,
    success: function(data){
    var array = $(data).find(‘return’).map(function () {
    return $(this).text();
    }).get(0);
    successcallback(array);
    },
    error: errorcallback
    });

    error shows
    Uncaught SyntaxError: Unexpected token <

    its not take xml tag how can i remove

    i used jsonp but i want to use json but json not hit web service

    Karan

    15 Oct 12 at 18:33

Leave a Reply