Building a JSON web service with Java and axis2

Source: Internet
Author: User
Building a JSON web service with Java and axis2january 23rd, 2009 ·
9 comments

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 ):
    <module ref="DynamicResponseHandler"/>
  5. Add the JSON message formatters to the axis2.xml:
    <messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONMessageFormatter"/><messageFormatter contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>
  6. Add JSON message builders to the axis2.xml:
    <messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONOMBuilder"/><messageBuilder contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>
  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 has ed a nullpointerexception. I haven't checked it, but I hope it helps you! Thanks Zeno!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.