How to use Axis2 JSON

Some time back, I implemented JSON support for Axis2 and I’ve explained about the architecture and usage of it here. But in addition to the details provided in this article, there is something that I’ve missed. I’m going to explain it in this post.

When I was implementing this feature for Axis2, the main objective was to make it easy and efficient to get the incoming JSON string from the service. That’s why I’ve used OMSourcedElementImpl with an OMDataSource. When a message comes in, JSONOMBuilder creates an OMSourcedElementImpl using JSONDataSource. So the OMElement is not built until someone forcefully do that and the JSON string can be directly accessed from the JSONDataSource without converting it to XML.

Due to this decision, the OMElement built from the incoming JSON message doesn’t know the namespace of the incoming payload. Therefore, if you try to dispatch a JSON message from the RPCMessageReceiver, it will be failed providing “namespace mismatch” error. Therefore, only the RawXMLInOutMessageReceiver can be used for JSON services. So you have to get an OMElement as the parameter for the method.

You can have a look at the JSONIntegrationTest to have an idea about this. All the other important steps are provided in the above article.

About these ads

About isurues
Age : 24 Date of Birth : 05.11.1984 Country : Sri Lanka

7 Responses to How to use Axis2 JSON

  1. Alan Ning says:

    Hi,

    I have been trying out the JSON support in Axis2 recently. Getting Axis2 to send JSON is relatively easy, but I can’t get Axis2 server to accept incoming JSON request with both JSONMessage and JSONBadgerfish builder/formatters.

    Looking over the JSONIntegrationTest example, it seems to format the content directly with OMElements, and does not use any of the data binding classes. Does Axis2 data binding play well this JSON feature?

    Thanks

    … Alan

  2. isurues says:

    Hi Alan,

    Currently Axis2 JSON can’t be used with data bindings. I’ve explained the reason for this in the above post. This is a limitation and we have to improve it to support data bindings.

    Currently you can only use RawXMLInOutMessageReceiver and your operation should accept an OMElement (XML payload) without going through any data bindings.

    Thanks,
    ~Isuru

  3. Alan Ning says:

    Isurues,

    This explains a lot. Thank you.

    … Alan

  4. WERDEX says:

    Hi,
    I have modified JSONBadgerfishBuilder to handle RPCMessageReceiver correctly by converting JSON/Badgerfish explicitly to SOAP:
    public OMElement processDocument(InputStream inputStream, String contentType,
    MessageContext messageContext) throws AxisFault {
    ….
    AbstractXMLInputFactory inputFactory = createFactory();
    XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(
    new JSONTokener(IOUtils.toString(reader)));
    OMNodeEx document = (OMNodeEx) new StAXOMBuilder(xmlReader).getDocumentElement();
    //removing parent
    document.setParent(null);
    //wrapping document with envelope
    SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope();
    SOAPBody body = soapEnvelope.getBody();
    body.addChild(document);

    soapEnvelope.build();


    }

  5. isurues says:

    Hi,

    Great, can you please create a JIRA and attach your patch for this in the Axis2 project? I’ll test it and apply the patch.

    Thanks,
    ~Isuru

  6. Datchayani says:

    I am getting this error after added JSON Message formatters and JSON Message builders to the axis2.xml
    URL:

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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: