Quantcast
Channel: Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 741

Example of Sender Rest Adapter in PI 7.4 - PUT Operation

$
0
0

Hi Everybody, Greetings from Colombia.  This is my first Blog.

This is a step by step example of how to setup a Sender RESTAdapter Channel (with the Operation PUT), and how to test it using the Chrome Plugin and a simple JAVA Class.  I won't explain how to build the Mapping and the other side of the integration.

 

System: SAP PI 7.4 SP11

 

This example will follow this workflow:

 

JAVA <-> PI RESTAdapter (JSON) <-> PI Mapping (XML) <-> PI JDBCAdapter (XML) <-> Oracle SP

 

Bussiness Case Description

A Java class consumes a REST service published in PI to check if an ID (nmDni) represents a risk to the company.

 

SETTINGS

 

Message Mapping

The datatype must have a field nmDni, named just like the parameter that we are going to send in the JSON payload.

blog_message_mapping.PNG

 

Communication Channel

We will be working with the Adapter Type REST in the communication channel.  Follow the next screenshots to set up the channel:

cc_01_header.PNG

General Tab

Here it's important to set the Element Name with the request Message Type, so the JSON structure can be recognized in the request

cc_01_1_general.PNG

Channel Selection Tab

cc_01_2_channel_selection.PNG

REST Resources Tab

Here it's very important to set the JSON Element that will match with the datatype defined in PI nmDni

cc_01_3_rest_resources.PNG

Rest Operation Tab

cc_01_4_rest_operation.PNG

 

The last 2 tabs are left empty

  • Operation Determination
  • Error Handling

 

Activate and assign the channel to the ICO in the Integration Builder.

 

TESTING

 

Ping

The ping on the channel, in the Communication Channel Monitor, you can get the endpoint and check the pattern of the parameter that we will use in the testing fase.

cc_10_ping.PNG

 

Chrome Plugin

Download and install the Advance REST Client plugin for Google Chrome.

Put the endpoint and select the PUT operation.  In the payload section paste this code:

 

{

"nmDni":"6183"

}

cc_30_1_chrome.PNG

 

Clic the "Send" button and the response should appear in JSON format

cc_30_2_chrome.PNG

 

Java Class

Here it's the Java class that will work to test the REST Service.  It's important to notice, that you need a PI user and password.  That user and password is encoded using Base64 library.

 

 

package com.prueba.rest;

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.HttpURLConnection;

import java.net.URL;

import sun.misc.BASE64Encoder;

 

public class PruebaRestMAIN {

 

    public static void main(String[] args) {

 

    String name = "pi_username";

        String password = "pi_password";

    String authString = name + ":" + password;

        String authStringEnc = new BASE64Encoder().encode(authString.getBytes());

        System.out.println("Auth string: " + authStringEnc);

 

        String line;

        StringBuffer jsonString = new StringBuffer();

        try {

            URL url = new URL("http://your_PI_domain:50000/RESTAdapter/riesgosput/1234");

 

            //escape the double quotes in json string

            String payload="{\"nmDni\":\"71333\"}";

 

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

 

            connection.setDoInput(true);

            connection.setDoOutput(true);

            connection.setRequestMethod("PUT");

            connection.setRequestProperty("Authorization", "Basic "+authStringEnc);

            connection.setRequestProperty("Accept", "application/json");

            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

         

            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

            writer.write(payload);

            writer.close();

            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            while ((line = br.readLine()) != null) {

                    jsonString.append(line);

            }

            br.close();

            connection.disconnect();

        } catch (Exception e) {

                throw new RuntimeException(e.getMessage());

        }

     

        System.out.println("Respuesta: "+jsonString.toString());

     

    }

}

 

PI MESSAGE MONITOR

In the PI message monitor you will see the 2 lines for each test.  And if you open the message you will see in the Payload tab the JSON message:

cc_20_message_monitor.PNG


I hope this helps in your way to test the RESTAdapter in PI.

Have a good day


Viewing all articles
Browse latest Browse all 741

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>