Many Oracle EBS RESTful services have been developed in recent projects. Friends from other project teams need to use Java to call the service. I don’t know how to call it? Remember that on a project, a simple example of using Java call to test Oracle EBS RESTful service is as follows:
1. Get RESTfull service connection:
Path: Integrated Information
Library- >Applications Technology->User Management->User Find the interface list: User->Name is User, internal name is FND_USER_PKG, as shown in the figure below:
Click "User"->REST Web Service- >View the WADL
XML The comparison between the content and the JSON content of Java is as follows:
2. Write a Java program to call Oracle EBS RESTful
2.1 Maven POM to load related packages
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.1.0</version>
</dependency>
2.2 Create a class: oracleRestInovk
package com.cxp;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import com.sun.jersey.core.util.Base64;
public class oracleRestInovk {
private static final String soapJson = "{\"TESTUSERNAME_Input\":{ "
+ " \"@xmlns\":\"http://xmlns.oracle.com/apps/fnd/soaprovider/plsql/rest/FND_USER_PKG/\","
+ " \"RESTHeader\":{ "
+ " \"@xmlns\":\"http://xmlns.oracle.com/apps/fnd/soaprovider/plsql/rest/FND_USER_PKG/\","
+ " \"Responsibility\":\"SCUX_INTERFACE_SUPERUSER\"," + " \"RespApplication\":\"SCUX\","
+ " \"SecurityGroup\":\"STANDARD\"," + " \"NLSLanguage\":\"AMERICAN\"," + " \"Org_Id\":\"\" "
+ " }, " + " \"InputParameters\":{ " + " \"X_USER_NAME\":\"SYSADMIN\" " + " }" + "}}";
/**
* 该方法使用由AOL登录服务返回的accessTokenName和accessToken值调用RESTfull服务
*/
public static void callRestfulApi(String restUrl, String tokenName, String tokenValue) throws IOException {
URL url = new URL(restUrl);
// 获取连接调用服务
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置 Http 请求方法
conn.setRequestMethod("POST");
// 设置 Http 头值
conn.setRequestProperty("Content-Type", "application/json");
// Adding the accessTokenName and accessToken as Cookies
conn.addRequestProperty("Cookie", tokenName + "=" + tokenValue);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Language", "en-US");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
// 发送请求
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
System.out.println("soapJson:" + soapJson);
wr.write(soapJson.toCharArray());
wr.flush();
wr.close();
conn.connect();
System.out.println("Response code - " + conn.getResponseCode());
// 获得请求响应
String response = null;
try {
response = readHttpResponse(conn);
} finally {
if (conn != null)
conn.disconnect();
}
// 输出响应内容
System.out.println("Response is : \n" + response);
}
/**
* 获取Access Token
*/
@SuppressWarnings("deprecation")
private static String[] getAccesToken(String baseUrl, String username, String passwd) throws Exception {
String rfUrl = baseUrl + "/login";
URL url = new URL(rfUrl);
// 获取连接调用服务
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String auth = username + ":" + passwd;
byte[] bytes = Base64.encode(auth);
String authorization = new String(bytes);
// 设置 Http 请求方法
conn.setRequestMethod("POST");
// 设置 Http 头相关值
conn.setRequestProperty("Authorization", "Basic " + authorization);
conn.setRequestProperty("Content-type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
String response = null;
// 获得请求响应
try {
response = readHttpResponse(conn);
} finally {
if (conn != null)
conn.disconnect();
}
System.out.println("response:" + response);
// 解析响应
JsonParser jp = null;
JsonNode root = null;
ObjectMapper mapper = new ObjectMapper();
try {
jp = mapper.getJsonFactory().createJsonParser(new ByteArrayInputStream(response.getBytes()));
jp.disableFeature(org.codehaus.jackson.JsonParser.Feature.AUTO_CLOSE_SOURCE);
root = jp.readValueAsTree();
} catch (JsonParseException jpe) {
jpe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
JsonNode dataNode = root.get("data");
JsonNode accessTokenNode = dataNode.get("accessToken");
String accessToken = accessTokenNode.getTextValue();
JsonNode accessTokenNameNode = dataNode.get("accessTokenName");
String accessTokenName = accessTokenNameNode.getTextValue();
return (new String[] { accessTokenName, accessToken });
}
/**
* 该方法读取服务器发送的响应并以字符串表示形式返回。
*/
private static String readHttpResponse(HttpURLConnection conn) {
InputStream is = null;
BufferedReader rd = null;
StringBuffer response = new StringBuffer();
try {
if (conn.getResponseCode() >= 400) {
is = conn.getErrorStream();
} else {
is = conn.getInputStream();
}
rd = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\n');
}
} catch (IOException ioe) {
response.append(ioe.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
if (rd != null) {
try {
rd.close();
} catch (Exception e) {
}
}
}
return (response.toString());
}
public static void main(String[] args) throws Exception {
String baseUrl = "http://<hostname>:<port>/webservices/rest";
String restUrl = baseUrl + "/FND_USER/testusername/";
System.out.println(restUrl);
// 调用AOL登陆服务获得 Access Token
String[] token = getAccesToken(baseUrl, "<EBS User Name>", "<password>");
// 输出Access Token名称和值
System.out.println("AOL Token : Name - " + token[0] + ", Value - " + token[1]);
// 使用Access Token调用RESTful服务
callRestfulApi(restUrl, token[0], token[1]);
}
}
As it involves sensitive information, the referee needs to modify the above code as follows:
Other issues: Note that <EBS User Name> must have the authority to call RESTful services.
The execution output is as follows: