This tutorial covers creating a RESTful Web Service and accessing the Web Service through an application in Application Express 5.1. It also covers consuming the Web Service using a REST client.
Time to Complete
Approximately 40 minutes.
Overview
Web Services enable applications to interact with one another over the web in a platform-neutral,language independent environment. In a typical Web Services scenario,a business application sends a request to a service at a given URL by using the protocol over HTTP. The service receives the request,processes it,and returns a response. Web Services are typically based on Simple Object Access Protocol (SOAP) or Representational State Transfer (REST) architectures. RESTful Web Services are result oriented. The scope of the Web Service is found in the URI and the method of the service is described by the HTTP method that is used such as GET,POST,PUT,and DELETE.
The RESTful Web Service Wizard is a set of pages in the sql Workshop area of Oracle Application Express that help you to create a RESTful Web Service declaratively. Once you have defined a RESTful Web Service,you can call it with a unique Uniform Resource Identifier (URI). RESTful Web Services are organized within Oracle APEX through a hierarchy of a module,a resource template and handlers within a template. The resource template includes a prefix for the URI,which is completed with a final portion of the URI defined by the handler.
This tutorial covers creating a RESTful Web Service declaratively using Oracle Application Express's sql Workshop tool,and then consuming the same service by creating an application and adding a Web Service Reference to the RESTful Web Service. The RESTful Web Service is also consumed using a REST client.
Please keep in mind the following while running this tutorial:
Logging into your Oracle Application Express workspace:Your Oracle Application Express workspace may reside in an on-premises Oracle Database or in Oracle Database Cloud Services. The login credentials differ depending on where your workspace is located:
Logging into Oracle Application Express in a Oracle Database Cloud Service: Go to the Oracle Help Center for Cloud,and select Platform and Infrastructure. From here,select your Database Cloud Service and the Get Started page will appear.
Logging in to Oracle Application Express on-premises: From your browser,go to the location of your on-premises installation of your Oracle Application Express workspace provided by your Workspace Administrator.
Application ID:Screenshots in this tutorial show a blurred Application ID. Your Application ID can be any value assigned automatically while creating the application.
Schema:If you are accessing an Oracle Application Express workspace in Database Schema Service,you have one schema assigned to you with a schema name that you cannot change. If you are accessing the workspace in an on-premises Oracle database,you may have more than one schema assigned to your workspace by the Oracle Application Express Instance Administrator.
What Do You Need?
Before starting this tutorial,you should:
Have access to an Oracle Database 11.2.x.x or later release,including Enterprise Edition and Express Edition (Oracle Database XE),either on-premises or in a Database Cloud Service.
Install Oracle Application Express Release 5.1 into your Oracle Database with RESTful Services configured in Oracle Application Express (for on-premises only).
Download and unzip thefiles.zipinto your working directory.
ExecuteCreate_Employees.sqlfrom the extracted files,to create required database objects.
Creating a RESTful Web Service
In this topic,you create a RESTful Web Service using RESTful Services tool in sql Workshop. The RESTful Web Service Wizard is a set of pages in sql Workshop that help you to create a new RESTful Web Service declaratively. The RESTful Web Service calls a specific sql statement in your database.
Creating a RESTful Web Service with GET and PUT Resource Handlers
To create a RESTful Web Service on the Employees table with sample GET and PUT service handlers,perform the below steps:
From the Oracle Application Express Home page,select thesql Workshoptab and selectRESTful Services.
A page loads with entries grouped under three different categories named RESTful Services Module,Resource Template,and Resource Handler. Under RESTful Services Module,enteremployeesfor Name,and scroll down further.
Under Resource Handler,selectGETfor Method,Queryfor Source Type,CSVfor Format. This identifies the HTTP method to be used for the Resource Handler. Enter the following sql query for Source,and clickCreate Module.
To test the behavior of the RESTful Service Handler,clickTest.
Note:If your screen does not show a Test button,please ensure that RESTful Services are configured in your Oracle Application Express installation properly.
SelectGETfor Method,Feedfor Source Type,andNofor Requires Secure Access. Under Source,enter the following sql query,and clickCreate. select employee_id,first_name from employees order by employee_id,first_name
The results are rendered in JSON format. Each item consists of a URI which contains the base URI from this RESTful Service,and the value of employee_id used as the parameter. For the Feed source type,the first column must be a unique identifier. It will be transformed into a hyperlink when this RESTful Service is called. In this example,employee_id is the first column and will turn into a hyperlink.
For example,in the screenshot shown below,the URI for an employee withemployee_id = 100ishttps://:<hostname>:<port>/ords/hr/employeesFeed/100
Note:The URI shown in this example is specific to the database On-Premise subscription used for executing this tutorial,and it might be different for you. The value of the URI also depends on whether you are performing this tutorial On-Premises or on a Cloud Service. In general,the URI formats are as follows:
On-Premises
http://localhost:<Port where ords is set up>/ords/<schema name>/<Resource Template Name>/<employee_id>
For example:http://localhost:9090/ords/hr/employeesFeed/100 where ords is set up on port9090during Oracle APEX installation,hris the schema name,employeesFeedis the Resource Template name,and100is the employee_id.
<Service URL as received in the Welcome email from Oracle Cloud>/<Resource Template Name>/<employee_id>
For example:https://databasetrial:<user>.db.us2.oraclecloudapps.com/hr/employeesFeed/100 where<user>.db.us2.oraclecloudapps.com/hr/is the Service URL for the cloud service subscription,and100is the employee_id.
The results are rendered in JSON format. Each item consists of a URI which contains the base URI from this RESTful Service,and the value of department_id used as the parameter. For the Feed source type,the first column must be unique identifier and will be transformed into a hyperlink when this RESTful Service is called. In this example,department_idis the first column and will turn into a hyperlink.
For example,the URI for an employee with department_id = 60 ishttp://<hostname>:<port>/ords/hr/employeesFeed/60?page=1where page=1 indicates that these results are part of page 1. If there are many records in the result set,the results can span across page 2 and so on.
Note:The URI shown in this example is specific to the database On-Premise service subscription used for executing this tutorial,the URI formats are as follows:
On-Premises
http://localhost:<Port where ords is set up>/ords/<schema name>/<Resource Template Name>/<department_id>?page=1
For example:http://localhost:9090/ords/hr/employeesFeed/60?page=1 where ords is set up on port9090during Oracle APEX installation,serif; padding: 3px; background-color: transparent; border-radius: 4px; line-height: 1.615;">hris the schema name,serif; padding: 3px; background-color: transparent; border-radius: 4px; line-height: 1.615;">employeesFeedis the Resource Template name and60is the department_id.
<Service URL as received in the Welcome email from Oracle Cloud>/<Resource Template Name>/<department_id>?page=1
For example:https://databasetrial:<user>.db.us2.oraclecloudapps.com/hr/employeesFeed/60?page=1 where<user>.db.us2.oraclecloudapps.com/hr/is the Service URL for the cloud service subscription,serif; padding: 3px; background-color: transparent; border-radius: 4px; line-height: 1.615;">employeesFeedis the Resource Template name,and60is the department_id.
Enteremployeesfor Name. SelectGETfor HTTP Method andNofor Basic Authentication. For the URL,enter the Web Reference URI for the GET Service Handler which was created in the first section of this tutorial.
As explained in the below table,your URI depends on the location of your Oracle Application Express instance,whether On-Premises or on a Database Cloud Service.
On-Premises
http://localhost:<Port where ords is set up>/ords/<schema name>/<Resource Template Name>/
For example:http://localhost:9090/ords/hr/employees/ where ords is set up on port9090during Oracle APEX installation,hris the schema name,employeesis the Resource Template name.
On a Cloud Service
<Service URL as received in the Welcome email from Oracle Cloud>/<Resource Template Name>/
For example:<user>.db.us2.oraclecloudapps.com/hr/employees/ wherehttps: databasetrial:<user>.db.us2.oraclecloudapps.com/hr/is the Service URL for the cloud service subscription andemployeesis the Resource Template name.
This RESTful Web Service does not require an HTTP Header parameter. So,scroll down and click theDelete Headericon.
Now,you have to define the REST Outputs. SelectTextfor Output Format.Entercommafor Parameter Delimiter,and\nfor New Record Delimiter. Under Output Parameters,enterEmployee IDfor Nameand1for Path. SelectStringfor Type,and clickAdd Parameter.
To fetch the details of an employee,selectGETas the Request Method. For the URL,whether On-Premises or on a Database Cloud Service. In this example,<hostname>:<port>/ords/hr/employees/employeesis the URL used for theOn-Premise machine.
On-Premises
http://localhost:<Port where ords is set up>/ords/<schema name>/<Resource Template Name>/<Resource Handler>
For example:http://localhost:9090/ords/hr/employees/employees where ords is set up on port9090during Oracle APEX installation,employeesis the Resource Template name andemployeesis the Resource Handler.
On a Cloud Service
<Service URL as received in the Welcome email from Oracle Cloud>/<Resource Template Name>/<Resource Handler>
For example:<user>.db.us2.oraclecloudapps.com/hr/employees/ employeeswherehttps: databasetrial:<user>.db.us2.oraclecloudapps.com/hr/is the Service URL for the cloud service subscription andemployeesis the Resource Template name andemployeesis the Resource Handler.
In the RESTClient page,enterPOSTas the request method,enter the same URL as that of the GET method,enter the following code in the Request Body,and clickSEND.
{ "first_name":"Supriya", "last_name":"Ananth", "email":"SUPANANT", "hire_date":"13-05-2001", "job_id":"AD_PRES" }Description of this image
Under Response,selectResponses Header. You see that the new employee's information is added into the Employees table. The newly created employee’s ID is returned back to the application.
In this topic,you create a RESTful Web Service using RESTful Services tool in sql Workshop. The RESTful Web Service Wizard is a set of pages in sql Workshop that help you to create a new RESTful Web Service declaratively. The RESTful Web Service calls a specific sql statement in your database.
Creating a RESTful Web Service with GET and PUT Resource Handlers
To create a RESTful Web Service on the Employees table with sample GET and PUT service handlers,perform the below steps:
From the Oracle Application Express Home page,select thesql Workshoptab and selectRESTful Services.
To test the behavior of the RESTful Service Handler,please ensure that RESTful Services are configured in your Oracle Application Express installation properly.
The results are rendered in JSON format. Each item consists of a URI which contains the base URI from this RESTful Service,the URI formats are as follows:
On-Premises
http://localhost:<Port where ords is set up>/ords/<schema name>/<Resource Template Name>/<employee_id>
For example:Box-sizing: border-Box; font-size: 13px; font-family: menlo,and100is the employee_id.
The results are rendered in JSON format. Each item consists of a URI which contains the base URI from this RESTful Service,the URI formats are as follows:
On-Premises
http://localhost:<Port where ords is set up>/ords/<schema name>/<Resource Template Name>/<department_id>?page=1
Enteremployeesfor Name. SelectGETfor HTTP Method andNofor Basic Authentication. For the URL,serif; padding: 3px; background-color: transparent; border-radius: 4px; line-height: 1.615;">
On-Premises
http://localhost:<Port where ords is set up>/ords/<schema name>/<Resource Template Name>/
For example:Box-sizing: border-Box; font-size: 13px; font-family: menlo,employeesis the Resource Template name.
On a Cloud Service
<Service URL as received in the Welcome email from Oracle Cloud>/<Resource Template Name>/
For example:https: databasetrial:<user>.db.us2.oraclecloudapps.com/hr/is the Service URL for the cloud service subscription andemployeesis the Resource Template name.
This RESTful Web Service does not require an HTTP Header parameter. So,scroll down and click theDelete Headericon.
To fetch the details of an employee,serif; padding: 3px; background-color: transparent; border-radius: 4px; line-height: 1.615;"><hostname>:<port>/ords/hr/employees/employeesis the URL used for theOn-Premise machine.
On-Premises
http://localhost:<Port where ords is set up>/ords/<schema name>/<Resource Template Name>/<Resource Handler>
For example:Box-sizing: border-Box; font-size: 13px; font-family: menlo,employeesis the Resource Template name andemployeesis the Resource Handler.
On a Cloud Service
<Service URL as received in the Welcome email from Oracle Cloud>/<Resource Template Name>/<Resource Handler>
For example:https: databasetrial:<user>.db.us2.oraclecloudapps.com/hr/is the Service URL for the cloud service subscription andemployeesis the Resource Template name andemployeesis the Resource Handler.
Under Response,selectResponses Header. You see that the new employee's information is added into the Employees table. The newly created employee’s ID is returned back to the application.