Matinal: Analysis of the whole process of SAP ABAP publishing HTTP RestFul service (4)

1. Concept

1.1. How SAP provides Http Service

If you want to use the SAP application server as an http service provider, you need to define a class, which must implement the IF_HTTP_EXTENSION interface. The IF_HTTP_EXTENSION interface has only one method, HANDLE_REQUEST. This customized class must implement the HANDLE_REQUEST method. The SERVER parameter of the HANDLE_REQUEST method is an http server object (of type IF_HTTP_SERVER interface of the ICF framework). The http server object has properties and methods for processing requests and responses.
Then create the service using SICF transaction code and set the handler to a custom class.
The client can access this service.

1.2. REST

This part is quoted from: What is REST, author: IT Cultivation Academy

It is the abbreviation of Resource Representational State Transfer: in layman’s terms, it means: resources transfer state in a certain form of expression in the network. Speaking separately:

1.Resource: Resource, that is, data (the core of the network).

2.Representational: some form of representation, such as JSON, XML, JPEG, etc.;

3.State Transfer: state change. Implemented via HTTP verbs.

REST describes a form of interaction between client and server in the network; REST itself is not practical, but what is practical is how to design a RESTful API (REST-style network interface);

In the RESTful API provided by Server, only nouns are used in the URL to specify resources, and verbs are not used in principle. “Resources” are the core of the REST architecture or the entire network processing.

Use verbs in the HTTP protocol to add and modify resources.

A representation of a resource is transferred between the Server and the Client, such as using JSON or XML to transfer text, or using JPG or WebP to transfer pictures, etc.

Use HTTP Status Code to pass server status information. For example, the most commonly used 200 means success, 500 means internal server error, etc.

The web side no longer uses the previous typical PHP or JSP architecture, but instead uses front-end rendering and incidental processing of simple business logic. The web side and server only use the API defined above to transfer data and change data status. The format is generally JSON.

Specific operation types for resources are represented by HTTP verbs. There are five commonly used HTTP verbs (the corresponding SQL commands are in brackets):
1.GET (SELECT): Get resources (one or more items) from the server
2.POST (CREATE): Create a new resource on the server
3.PUT (UPDATE): Update resources on the server (the client provides the complete changed resources)
4.PATCH (UPDATE): Update resources on the server (client provides changed attributes)
5.DELETE (DELETE): Delete resources from the server.

for example:
GET /zoos: List all zoos
POST /zoos: Create a new zoo
GET /zoos/ID: Get information about a specified zoo
PUT /zoos/ID: Update the information of a specified zoo (provide all the information of the zoo
PATCH /zoos/ID: Update the information of a specified zoo (provide partial information of the zoo)
DELETE /zoos/ID: delete a zoo
GET /zoos/ID/animals: List all animals in a specified zoo
DELETE /zoos/ID/animals/ID: Delete the specified animal from a specified zoo

2. Tools

SAP
SOAPUI 

3. Create interface

3.1, SE24 interface class

Activate, then add interface:IF_HTTP_EXTENSION and activate. IF_HTTP_EXTENSION is a standard interface, just fill it in, then click the method tab, double-click the first line of implementation code

3.2 Implementing the interface

Add interface:IF_HTTP_EXTENSION,. IF_HTTP_EXTENSION is a standard interface, just fill it in, then click the method tab, double-click the first line of implementation code, and then activate it.

The content of the class is as follows:

CLASS zcl_rest_mytest DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.

    INTERFACES if_http_extension .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

* Official account: matinal

CLASS ZCL_REST_MYTEST IMPLEMENTATION.


* <SIGNATURE>------------------------------------------------- ----------------------------------------------- +
* | Instance Public Method ZCL_REST_MYTEST->IF_HTTP_EXTENSION~HANDLE_REQUEST
* +------------------------------------------------ --------------------------------------------------+
* | [--->] SERVER TYPE REF TO IF_HTTP_SERVER
* +------------------------------------------------ ----------------------------------</SIGNATURE>
  METHOD if_http_extension~handle_request.
    DATA: lo_json_ser TYPE REF TO cl_trex_json_serializer,
          lo_json_des TYPE REF TO cl_trex_json_deserializer.
    DATA: json_string TYPE string.
    DATA:gt_t005u TYPE TABLE OF t005u.

    DATA: lv_input_json TYPE string .
    "Get the incoming json content
    lv_input_json = server->request->if_http_entity~get_cdata( )."

    "Set the return data format
    CALL METHOD server->response->if_http_entity~set_content_type
      EXPORTING
        content_type = 'application/json'.
    "Query data
    SELECT * INTO TABLE gt_t005u FROM t005u UP TO 20 ROWS WHERE spras = '1' AND land1 = 'CN'.

    "Convert data to json format
    CREATE OBJECT lo_json_ser
      EXPORTING
        data = gt_t005u[].

    "Fixed writing method
    CALL METHOD lo_json_ser->serialize.

    "Get the converted data and output it
    CALL METHOD lo_json_ser->get_data
      RECEIVING
        rval = json_string.
    server->response->set_cdata(
            EXPORTING
              data = json_string " Character data
          ).

  ENDMETHOD.
ENDCLASS.
3.3 SICF defines REST services

Create a child node under the default_host/sap node using transaction code SICF, use the Z_SIMPLE_REQ_HANDLER class and activate the method.

Create a new child element here by selecting

4. Test interface

4.1. Enter the test address

4.2. Input parameters

4.3. Breakpoint debugging

Set an external breakpoint and then perform a POST

Use get_cdata() in sap to obtain the content (json string) passed in by post, and obtain the required parameters after parsing the JSON.

You can see the returned json information in SOAPUI