最近項目開發許多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服務的權限。
執行輸出結果如下: