By Kumar Abhishek Shahi
Numerous legacy systems that have Procedural Language/Structured Query Language (PL/SQL) code containing complex business logic are in migration. The business need is to reuse them and create an interface to expose them to various other systems..
Publishing the PL/SQL package as a Web Service is a simple solution to such scenarios:
- It reduces development effort because existing code can be re-used.
- The complex logic which has been put in the PL/SQL code which has been running over years and tested should not be re-written.
- Web Services built on top of it can be consumed by any application i.e. Web Application, Desktop Application, Mobile Interface as well as Portals and other Packaged Applications.
This Article focuses on:
- How we can publish a PL/SQL Package as Web Service using JDeveloper 12c.
- How we can consume those services in an ADF Application.
- JDeveloper 12c
- Oracle Database XE
- Basic PL/SQL knowledge
I have taken a simple use case of converting USD Amount to INR Amount.
In this exercise,
- I have created a sample PL/SQL package using DB Explorer in JDeveloper.
- Package has been exposed as a Web Service and tested using HTTP Analyzer.
- At the end, there is an ADF Web Application which consumes Web Service and displays it a beautiful UI using JSF Page.
Creating a PL/SQL Package to Convert USD to INR
- Open DB Explorer.
- Create a DB connection.
Note: Make sure HR schema is unlocked. Refer to Unlock HR Schema for more detail.
- Open SQL Worksheet to run SQL scripts.
- Create a package utility with a function to convert USD to equivalent INR amount.
create or replace PACKAGE Utility AS FUNCTION UsdToInr(usdAmt number) return number; END Utility;
Create package and function body.
create or replace PACKAGE BODY Utility AS FUNCTION UsdToInr(usdAmt IN number) RETURN number IS inrAmt number(5) := 0; BEGIN SELECT usdAmt*60 into inrAmt from dual; RETURN inrAmt; END UsdToInr; END Utility;
Publishing Package as a Web Service
Create custom application.
- Specify application name and package structure.
- Select Project Features “SOAP Web Service” and add it to the selected by clicking ">"
Click “Finish” to create application.
- To publish SQL Package as TOPLink DB Web Service Provider, Right Click on Project-> Click “From Gallery”
- Select TOPLink DB Web Service Provider and click OK.
- Go through the Web Service Provider Wizard
- Select PL/SQL Web Service Provider. (We are using SQL package to expose it as a Web Service.) Click Next.
- Specify required properties: Database Connection Name, Database Package, Web Service Name, etc. Click Next.
- Select the function that needs to be exposed as service.
- Click Finish. It will take a while to generate Web Service using Package.
Testing PL/SQL Web Service
- Right click on DBWSProvider.java.
- Select Test Web Service from the pop-up menu.
- HTTPAnalyzer will open. Enter USDAmount = 50 and click “Send Request.”
You can see that the result is displayed as 3000.
Copy the Web Service Definition Language (WSDL) URL for future use:
Creating User Interface to Consume Web Service
Create new ADF Fusion Web Application.
Specify Application Name and package structure.
Click Next and then Finish. An ADF Web Application will generate.
Create New Web Service Data Control using copied WSDL.
Create New Data Control. Select Web Service Data Control using SOA/REST.
Specify DC Name and WSDL. Check Copy WSDL Locally so that there is no need to hit the server to see whether WSDL is readable or not.
Select Response Format.
Click Finish to generate Data Control out of WSDL.
Create New JSF Page
Create JSF page.
Add one PanelGroupLayout and one panel box to it.
Expand DataControl and drag usdAmt as inputText. Change label to “Amount in USD.”
Drag BigDecimal as outputText. Change label to “Amount in INR.”
Drag Data Control as method->AdfButton. Change label to “Convert.” .Change ActionListener to Action for AdfButton.
- Save and run!
Exposing PL/SQL Package as Web Service is a proven solution which could be achieved using JDeveloper 12c. JDeveloper 12c enables developers to generate and consume Web Service from one IDE. PL/SQL Web Services offers greater flexibility, scalability and reuse of existing resources.
About the Author
Kumar Abhishek Shahi is a principal consultant at Oracle Corporation, Working as an Architect for Webcenter Portal and ADF-based solutions, including technical architecture evaluation, performance tuning, code review, creating POCs and developing full-fledged solutions.