20-Nov-2024
How to call RESTAPI from JDeveloper 10.1.3.5
How to call RESTAPI from JDeveloper 10.1.3.5
Purpose:
In this artical we will see how to invoke a GET RESTFUL service.
For this example we will call FND_USER_PKG Rest API this RESTAPI comes by default in Oracle R12.2 Integrated SOA Gateway.
For more details please refer to
"Steps to test FND_USER_PKG Rest API using any REST client like Postman, SOAP UI or ARC (Deployment Process Included) (Doc ID 2649820.1)"
Prerequisites:
Need below softwares downloaded. These libraries comes with pre setup of souce code that will help to make RESTAPI call. e..g.: HttpClient
commons-codec-1.9.jar
commons-logging-1.2.jar
httpclient-4.5.2.jar
httpcore-4.4.4.jar
Need below Java 1.7 installed (since RESTAPI requires TLSv1.2 Java 1.6 doesnot support so required to use 1.7) i have used JDK 1.7.0_343 for my testing.
I have provided link to download the sofware and the source code for your reference purpose only.
Create OA project in JDev and create a java class
Import above mentioned libaries in the project property.After below code is compiled in default J2SE Version comes by default JDev 1.6.0_23 change J2SE to 1.7 version because RESTAPI cannot be called from version 1.6 since it can support only TLSv1.
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import javax.xml.bind.DatatypeConverter;
import oracle.jdbc.pool.OracleDataSource;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class surenTutorialProg {
public surenTutorialProg() {
}
public static void main(String[] args) throws ClientProtocolException, IOException{
HttpClient httpClient = HttpClientBuilder.create().build();
String auth = getBasicAuthenticationHeader("username","password");
System.out.println(auth);
HttpGet request = new HttpGet("https://hostname:443/webservices/rest/hello/testusername/?X_USER_NAME=SSUBRAM");
request.addHeader("Authorization", auth);
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
request.addHeader("content-type", "application/json");
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
System.out.println(request);
System.out.println(content);
// incase if the request and response need to be stored in database.
storeinDB(request.toString(),content);
}
private static final String getBasicAuthenticationHeader(String username, String password) {
String valueToEncode = username + ":" + password;
byte[] b = valueToEncode.getBytes();
String encoded = DatatypeConverter.printBase64Binary(b);
return "Basic " + encoded;
}
private static final void storeinDB (String request, String response) {
Connection con=null;
try {
String url_primary = "jdbc:oracle:thin:@hostname:port/sid";
String user = "apps";
String password = "pwd";
OracleDataSource ods1 = new OracleDataSource();
ods1.setURL(url_primary);
ods1.setUser(user);
ods1.setPassword(password);
Connection conn1 = ods1.getConnection();
String query = "{call suren_eclipse11(?,?)}";
CallableStatement statement = conn1.prepareCall(query);
Clob clob = conn1.createClob();
clob.setString(1, response );
System.out.println(clob);
statement.setClob(1, clob);
statement.setString(2, request);
statement.execute();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Output will look like below:
Basic Y3RkaWFwaTp3sdfds1234ZWxjb21lMQ==
GET https://host:443/webservices/rest/hello/testusername/?X_USER_NAME=SSUBRAM HTTP/1.1
{
"OutputParameters" : {
"@xmlns" : "http://xmlns.oracle.com/apps/fnd/rest/hello/testusername/",
"@xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
"TESTUSERNAME" : "2"
}
}
No comments:
Post a Comment