Adding JSONP support to a Web Service

With the Turmeric SOA runtime framework, and in particular the binding framework, a web service can return data in a variety of formats.  This is accomplished through the use of a plugable request/response serialization/deserialization framework.   As of version 1.0, it can support SOAP, XML, JSON, Name Value, and XMLInfoSet.  With the soon to be released, 1.1 version, it will add Google ProtoBuf support as well.

Natively it doesn’t support JSONP, which is used by many javascript applications.   However, you can easily add this support to your web service by adding a filter in your web.xml for your service.   Hidden out on Google Code is a project called, jsonp-java.   It basically looks for response types that have the application/json, text/json, application/x-json, and other related mime-types.   If there is a callback parameter specified in the request, it will wrap the JSON data returned with the function name to be called.

To add this support to a Turmeric SOA web service or any web service that returns json, add the following code to your web.xml.




If you need to support other mime types, add an appropriate mime mapping to the web.xml. If you are using maven to build your WAR files, then you will need to add the jsonp.jar as a dependency during your war creation.


Unfortunately the jsonp jar file is not in maven central so you will need to checkout the code and install it into your local repository. Just run a mvn clean install after checking out the project. The code currently is only available in a Subversion repository, but I’ll put my git version of the repository online shortly at github.

Once everything is setup and deployed. To have a JSON request return as a JSONP request with Turmeric, you can add a jsonpCallback request parameter as part of your URL request.


Set the jsonpCallback query string to the name of the function that should be executed. When the response is returned, the JSON data will be wrapped with the function to be executed on the callback.

    "jsonns.ns2": "",
    "jsonns.ns3": "",
    "": "",
    "jsonns.xs": "",
    "jsonns.xsi": "",
    "ns2.PullMessageResponse": {
        "ns2.payload": {
            "ns2.content": [
                    "@id": "1"

The reason for the funky JSON, is that Turmeric SOA supports it’s own flavor of namespaces for JSON. Useful for dealing with existing XML formats that also need to served in a JSON format. There are multiple ways to handle this and the above representation is just another flavor. More on this in a future post.

The jsonp filter can be used with any web service that can return JSON data, it just needs to have the jsonpCallback query parameter set, so this isn’t limited to the Turmeric SOA framework.

This entry was posted in json, open source, turmeric, web services. Bookmark the permalink.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s