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.


  <filter>
      <display-name>jsonp</display-name>
      <filter-name>jsonp</filter-name>
      <filter-class>org.jsonp.JsonpFilter</filter-class>
      <init-param>
         <param-name>jsonp</param-name>
         <param-value>jsonpCallback</param-value>
      </init-param>
      <init-param>
         <param-name>json-mime-types</param-name>
         <param-value>application/json</param-value>
      </init-param>
   </filter>

   <filter-mapping>
      <filter-name>jsonp</filter-name>
      <url-pattern>*</url-pattern>
   </filter-mapping>


   <mime-mapping>
      <extension>json</extension>
      <mime-type>application/json</mime-type>
   </mime-mapping>

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.

      <dependency>
         <groupId>org.jsonp</groupId>
         <artifactId>jsonp</artifactId>
         <version>1.0.1</version>
      </dependency>

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.

localhost:8083/starws/StarWebServiceV1?X-TURMERIC-OPERATION-NAME=pullMessage&X-TURMERIC-RESPONSE-DATA-FORMAT=JSON&jsonpCallback=someFunction

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.

someFunction({
    "jsonns.ns2": "http://www.starstandards.org/webservices/2009/transport",
    "jsonns.ns3": "http://www.starstandards.org/webservices/2009/transport/bindings",
    "jsonns.ms": "http://www.ebayopensource.org/turmeric/common/v1/types",
    "jsonns.xs": "http://www.w3.org/2001/XMLSchema",
    "jsonns.xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "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.

Advertisements
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:

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