Preparing a sample Db2 native REST service

Enable and create Db2 native REST services that use the Db2 sample tables. These services are then used by the sample OpenAPI definition, EmployeesApi.yaml, that's part of the sample-db2-api download.

Before you begin

This procedure is one of the prerequisites for creating a new z/OS® Connect API.

About this task

This topic is applicable to: Content applicable to Db2
administrators

You create two Db2 native REST services:
  • getEmployees - Returns a list of employees.
  • getEmployee - Returns the details of an individual employee.
Optionally create three Db2 native REST services used in z/OS Connect samples:
  • addEmployee - Add the details of an individual employee.
  • updateEmployee - Update the details of an individual employee.
  • deleteEmployee - Removes the details of an individual employee.

These services are created by using the Db2 Service Manager native REST service. Alternatively, the BIND SERVICE subcommand can be used to create a new Db2 native REST service. For each request, replace <db2_host_name> with the hostname of your Db2 instance and <db2_port> with the port of your Db2 instance. You might also need to include authentication credentials such as the Authorization HTTP header.

The services that are created in this sample scenario use the default Db2 collection SYSIBMSERVICE.

Procedure

  1. Ask your Db2 system administrator to create the Db2 sample tables and enable the creation of Db2 native REST services by following this procedure.
    1. Create the Db2 sample tables by running the Db2 installation sample jobs DSNTEJ1 and DSNTEJ7.
      The full procedure to install the Db2 samples tables is documented in the Db2 sample table topic in the Db2 for z/OS documentation.
    2. Enable Db2 native REST service creation.
      Follow the steps documented in Enabling Db2 REST services in the Db2 for z/OS documentation.
      Note: You must apply the PTF for Db2 APAR PI98649: New function update of Db2 native REST services support.
  2. Create the getEmployees service.
    Using a REST client, send an HTTP POST request to https://<db2_host_name>:<db2_port>/services/DB2ServiceManager with header Content-Type: application/json and the following body:
    {
        "requestType": "createService",
        "sqlStmt": "SELECT EMPNO, FIRSTNME, MIDINIT, LASTNAME, WORKDEPT, PHONENO, HIREDATE, JOB, EDLEVEL, SEX, BIRTHDATE, SALARY, BONUS, COMM FROM DSN81210.EMP WHERE WORKDEPT LIKE :WORKDEPT AND JOB LIKE :JOB",
        "serviceName": "getEmployees",
        "description": "Gets the details of all employee.",
        "collectionID": "SYSIBMSERVICE"
    }

    If the Db2 native REST service is created successfully, an HTTP Created (201) response code is returned.

  3. Create the getEmployee service.
    Using a REST client, send an HTTP POST request to https://<db2_host_name>:<db2_port>/services/DB2ServiceManager with header Content-Type: application/json and the following body:
    {
        "requestType": "createService",
        "sqlStmt": "SELECT EMPNO, FIRSTNME, MIDINIT, LASTNAME, WORKDEPT, PHONENO, HIREDATE, JOB, EDLEVEL, SEX, BIRTHDATE, SALARY, BONUS, COMM FROM DSN81210.EMP WHERE EMPNO = :EMPNO",
        "serviceName": "getEmployee",
        "description": "Gets the details of an individual employee.",
        "collectionID": "SYSIBMSERVICE"
    }

    If the Db2 native REST service is created successfully, an HTTP Created (201) response code is returned.

  4. Optional: Create the addEmployee service.
    Using a REST client, send an HTTP POST request to https://<db2_host_name>:<db2_port>/services/DB2ServiceManager with header Content-Type: application/json and the following body:
    {
        "requestType": "createService",
        "sqlStmt": "INSERT INTO DSN81210.EMP (EMPNO, FIRSTNME, MIDINIT, LASTNAME, WORKDEPT, PHONENO, HIREDATE, JOB, EDLEVEL, SEX, BIRTHDATE, SALARY, BONUS, COMM) VALUES (:EMPNO, :FIRSTNME, :MIDINIT, :LASTNAME, :WORKDEPT, :PHONENO, :HIREDATE, :JOB, :EDLEVEL, :SEX, :BIRTHDATE, :SALARY, :BONUS, :COMM)",
        "serviceName": "addEmployee",
        "description": "Add the details of an individual employee.",
        "collectionID": "SYSIBMSERVICE"
    }

    If the Db2 native REST service is created successfully, an HTTP Created (201) response code is returned.

  5. Optional: Create the updateEmployee service.
    Using a REST client, send an HTTP POST request to https://<db2_host_name>:<db2_port>/services/DB2ServiceManager with header Content-Type: application/json and the following body:
    {
        "requestType": "createService",
        "sqlStmt": "UPDATE DSN81210.EMP SET (EMPNO, FIRSTNME, MIDINIT, LASTNAME, WORKDEPT, PHONENO, HIREDATE, JOB, EDLEVEL, SEX, BIRTHDATE, SALARY, BONUS, COMM) = (:EMPNO, :FIRSTNME, :MIDINIT, :LASTNAME, :WORKDEPT, :PHONENO, :HIREDATE, :JOB, :EDLEVEL, :SEX, :BIRTHDATE, :SALARY, :BONUS, :COMM) WHERE EMPNO = :EMPNO",
        "serviceName": "updateEmployee",
        "description": "Update the details of an individual employee.",
        "collectionID": "SYSIBMSERVICE"
    }

    If the Db2 native REST service is created successfully, an HTTP Created (201) response code is returned.

  6. Optional: Create the deleteEmployee service.
    Using a REST client, send an HTTP POST request to https://<db2_host_name>:<db2_port>/services/DB2ServiceManager with header Content-Type: application/json and the following body:
    {
        "requestType": "createService",
        "sqlStmt": "DELETE FROM DSN81210.EMP WHERE EMPNO = :EMPNO",
        "serviceName": "deleteEmployee",
        "description": "Removes the details of an individual employee.",
        "collectionID": "SYSIBMSERVICE"
    }

    If the Db2 native REST service is created successfully, an HTTP Created (201) response code is returned.

Results

getEmployee, and getEmployees are Db2 native REST services, created as part of the SYSIBMSERVICE collection in Db2.

What to do next

Test that the Db2 native REST services are created correctly. See Testing the sample Db2 native REST services.