Advertisements



Sending JSON payload requests from BPEL

In this blog post, I am sharing sample code that demonstrates the usage of JSON in BPEL context. The code is refactored into 2 projects containing the java utility code and  a sample BPEL process. The code can be downloaded from here.

Demonstrates:

Java Utility Code:

  • Using cookies to pass authentication info (username and password).
  • Sending XML payload as URL parameters using intermediate conversion to JSON for GET request.
  • Sending HTTP request through HTTP proxy.
  • Sending JSON or XML payload in the POST request.

BPEL Process:

  • Using BPEL preferences to store any configurable information.
  • Using java embedding activity to call the external java code. The jar file has to be kept in SCA-INF/lib to refer to java code.
  • Using Replay activity. The fault policy mechanism can also be used for retry functionality, if we throw BPEL related faults from java code.
  • Converting JSON to XML and vice-versa using java utility routines in BPEL.
  • An example on creating XSD similar to JSON data format representing the service response so that XPath expressions can be used in BPEL.

Does not cover:

  • HTTP Basic Authentication
  • Using XML APIs (like JAXP) to convert input XML to URL parameters
  • URL encoding while calling HTTP GET method
  • Using JAX-RS to invoke REST based services

FYI:

  • XMLToJSONUtil.java in the code zip file is taken from Biemond’s blog.
  • The libraries used for JSON processing can be downloaded from here .
  • JSR – 353 Java API for JSON Processing is closed for public review.

The code is uploaded for reference and  this may not be the only way to achieve this.

Advertisements

6 Responses to “Sending JSON payload requests from BPEL”


  1. 1 MaryLucia March 28, 2013 at 2:06 PM

    Thanks, it works now nicely. I would need to send a JSON message where not all values are string, but some needs to be numbers, like following:

    {“lat”:60.3378,”lon”:22.2479}
    Do you know if this would be possible with java utility routines?

    • 2 svgonugu March 28, 2013 at 2:22 PM

      The source code given in Biemond’s blog (i gave the link in this post) would help you.

      • 3 MaryLucia April 3, 2013 at 2:18 PM

        Thanks. In the source code given, what ”type” input used in convert(), and afterward in xmlToJson() does represent? Could you open a bit the code?

        • 4 svgonugu April 4, 2013 at 6:04 PM

          There are 2 types Employees and Orders which are used in the sample code. While converting xml to json, the converter interface gets the string and write the correct object type (float or date). This can be observed in changeType method of Employess.java and Orders.java. The changeType method in these classes will be called using the code:
          dynamicConvertorClass = Class.forName(“nl.whitehorses.json.convertor.”+type.substring(0, 1).toUpperCase() + type.substring(1).toLowerCase());
          clazzInst = dynamicConvertorClass.newInstance();
          m = dynamicConvertorClass.getMethod(“changeType”, new Class[] { JSONObject.class });

          And this will be called in the flow when the following statement is executed which is also given in the sample code (XmlToJson.java).
          xmlToJson.xmlToJson(file,”employees”)

  2. 5 MaryLucia March 22, 2013 at 8:06 PM

    Thank you for this great post. I’ve tried your sample code and encountered this error:

    java.lang.NoClassDefFoundError cause: {net/sf/json/xml/XMLSerializer}

    Checked the project properties and it seems that
    json-lib-2.4-jdk15 and related libraries are in class-path of RestServiceUtilities project. But still this class is missing. Could you help on this?

    • 6 svgonugu March 23, 2013 at 4:18 PM

      Verify the library path it’s referring to. It might be referring to the path which is non-existing (my local machine). If that’s the case, you can download the jars using the link given in the post and add them to project again.


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




Advertisements

Pages

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 349 other followers

Enter your email address to follow this blog and receive notifications of new posts by email.