最近项目开发许多Oracle EBS RESTful服务,其他项目组朋友需要用Java调用该服务,不知如何调用? 记得在某项目上,使用Java调用测试Oracle EBS RESTful服务简单的实例如下:
一、获得RESTfull服务连接:
Path:集成信息库->Applications Technology->User Management->用户
找到接口列表:用户 ->名称为User,内部名称为FND_USER_PKG,如下图:
点击"User"->REST Web服务->查看WADL
该XML内容与Java 的JSON内容比较如下:
二、编写Java程序调用Oracle EBS RESTful
2.1 Maven POM加载相关包
<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 创建类: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]);
}
}
由于涉及敏感信息,参考者需求对以上代码修改如下:
其它问题:注意<EBS User Name>必须要用调用RESTful服务的权限。
执行输出结果如下: