This blog is part of a collection of blog entries that shows architectural concepts and configuration of the SAP PI REST Adapter. We also added some sample scenarios to make it easier for you to understand how your scenario can be implemented using the PI REST Adapter.
If you haven’t done so far, best is to start with the very first blog PI Rest Adapter - Don't be afraid within the blog series covering the concepts of the REST adapter. A complete list of all blog entries can be accessed from here PI REST Adapter - Blog Overview.
The current blog shows you along a sample scenario how to convert JSON to XML format and vice versa.
Scenario
A travel agency likes to gather flight details from an airline calling the remote function module BAPI_FLIGHT_GETDETAIL, and expose this interface as RESTful service. The format of the RESTful service should be JSON and hence needs to be converted into XML format before mapped to the BAPI structure.
In the SAP Process Integration Designer perspective of the NetWeaver Developer Studio (NWDS), I have defined an Integration Flow with a REST sender channel, an operation mapping that maps the outbound XML format into the BAPI structure, and an RFC receiver adapter. The conversion from JSON to XML is done within the REST sender adapter.
Image may be NSFW.
Clik here to view.
In the following, let’s focus on how the sender adapter needs to be configured.
Configuring the REST sender adapter
Double-click on the sender channel of type REST, and switch to the General tab below the Adapter-Specific tab. As data format of the input message choose JSON from the drop down menu. Within PI, the format of the payload needs to be XML, so select the Convert to XML check-box.
A sample JSON input message looks as follows:
{
"Agency": "110",
"FlightDetails": {
"AirlineID": "LH",
"ConnID": "0400",
"Date": "2014-09-13"
},
}
The two JSON elements Agency and FlightDetails are both on the very first level of hierarchy which is allowed for JSON however not for XML. XML messages can have one root element only. If the JSON payload is converted into XML, this would not comply with the XML specification. So, we need to add a wrapper root element to the converted XML structure. As Element Name and Element Namespace we enter the name and namespace of the message type of our outbound service interface, here flight_query with namespace http://demo/hh/rest/flights.
The scenario runs synchronously, so select Best Effort from the drop down menu of the Quality of Service.
The response of the BAPI call is in XML format within PI, the agency however expects the response being in JSON format. So, we choose JSON as data format of the output message, and select the check box Convert XML Payload to JSON. Furthermore we select the Strip Outer Element check box to remove the message type element from the XML payload.
Image may be NSFW.
Clik here to view.
Switch to tab Channel Selection, and select the Specify Endpoint check box. As custom static endpoint enter /demo/flight.
Image may be NSFW.
Clik here to view.
Switch to the REST Resources tab. Here, we stick to a static endpoint URL, so we just enter / as pattern.
Image may be NSFW.
Clik here to view.
Running the scenario
For testing the scenario you can use the Advanced REST Client Application in the Google Chrome browser. The endpoint URL of your RESTful service starts with http://<host>:<port>/RESTAdapter with host and port of the SAP PI system, followed by what you have defined in the sender channel, here /demo/flight.
Enter a sample JSON payload like the one above, and select the Send button.
Image may be NSFW.
Clik here to view.
The response of the service call provides the flight details such as destinations and schedule in JSON format.
Image may be NSFW.
Clik here to view.
In message monitoring, you can download the payload of the request and response assuming that you have switched on logging. You can see that for the request message the wrapper element, i.e., the message type element has been added.
Image may be NSFW.
Clik here to view.
The response in XML format also contains the message type as root element which is then removed when converted into JSON.
Image may be NSFW.
Clik here to view.
I hope this blog was helpful to understand the JSON conversion capabilities of the SAP PI REST adapter. If you like to learn more, check out the other blogs in the series, accessible from the main blog PI REST Adapter - Blog Overview.