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.
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?
The source code given in Biemond’s blog (i gave the link in this post) would help you.
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?
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”)
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?
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.