Providing Enterprise Data Simply via SAP BSP - Data Retrieval in JSON


Overview

In the previous post, we built a very simple BSP application that we used to retrieve a simple, hard-coded JSON. Today, we go ahead, and talk about how we can produce this hard-coded JSON in code. In order to do this, I'm going to introduce you the Event Handlers in Pages with Flow Logic.

Today lessons

What are the available Event Handlers
How to produce JSON in ABAP 7.4


Event Handlers

There are six event handlers available for pages with flow logic that are the followings: OnCreate, OnRequest, OnInitialization, OnManipulation, OnInputProcessing, OnInputProcessing, and OnDestroy.

  • OnCreate: is called first, and we usually use it for initializing our data and objects,
  • OnRequest: is called whenever there is a user request from a page,
  • OnInitialization: is called first when processing a page, and we implement the data retrieval in this handler,
  • OnManipulation: is used to post-process the HTTP data stream,
  • OnInputProcessing: is called when a user dialog occurs, and we can implement input checks and trigger the navigation to the next page,
  • OnDestroy: is called right before the page instance is deleted.

Alright! Now, as you are familiar with the different event handlers, and their roles in the flow logic, it's time to implement the data retrieval for our BSP app.


Delete Hard-Coded JSON

Our first task is to delete the hard-coded JSON from the layout, since from now on we won't need this, because we will get this JSON dynamically from code. If you're ready, let's save and activate it!


Implement OnInitialization

As you remember, to implement the data retrieval, we need to use the event handler, OnInitialization that you can find on the tab, Event Handler. Here, let's select it from the drop-down list.


First of all, we need some data that we query from the table, named SPFLI. For now, we are going to select only two rows, but later on (when we will deal with parameters) we will extend this logic. If you are not familiar with the following syntax below, then you can read about it here: Inline Declaration.

SELECT * FROM spfli INTO TABLE @DATA(flight_schedules) UP TO 2 ROWS.


To provide the data in JSON format, we are going to instantiate a JSON writer object using the method of the cl_sxml_string_writer class, named create(), with the parameter if_sxml=>co_xt_json .

SELECT * FROM spfli INTO TABLE @DATA(flight_schedules) UP TO 2 ROWS.

DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).


Using the received JSON writer, we are going to use the Indentity Transformation that is actually an XSLT transformation delivered by SAP, and will transform our tabular flight schedules into JSON format.

SELECT * FROM spfli INTO TABLE @DATA(flight_schedules) UP TO 2 ROWS.

DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).

TRY.
  CALL TRANSFORMATION id
    SOURCE flightschedules = flight_schedules
    RESULT XML json_writer.

  CATCH cx_xslt_format_error INTO DATA(xslt_format_error).
    " error handling
ENDTRY.


At last, we are going to place our JSON into the body of the response with the help of set_data() method. That's it!

SELECT * FROM spfli INTO TABLE @DATA(flight_schedules) UP TO 2 ROWS.

DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).

TRY.
  CALL TRANSFORMATION id
    SOURCE flightschedules = flight_schedules
    RESULT XML json_writer.

  CATCH cx_xslt_format_error INTO DATA(xslt_format_error).
    " error handling
ENDTRY.

CALL METHOD response->if_http_entity~set_data
  EXPORTING
    data = json_writer->get_output( ).


Test Your BSP Application

Now comes the best part of building something, running the app itself! For this, simply let's right-click on your BSP application and choose the option, Test! or let's paste the following URL into your browser:

http://xxxx:xxxx/sap/bc/bsp/sap/zflights/schedules


Summary

Where are we now? Right now, we have a BSP application that is capable to retrieve flight schedules tabular data in JSON format. The applied approach is the following:

  • query data from table, SPFLI,
  • transform it to JSON format using the Identity Transformation,
  • place the JSON into the body of the response.


Have you got the JSON message again after running your BSP app? I hope, you could follow me, and everything went fine. If not, then don't hesitate to leave me a comment below!


blog comments powered by Disqus