2021-01-22

Linux下Oracle11g调用http接口和json解析步骤

 

授权及导入第三方jar

1、  登录root

#su - oracle

#cd $ORALE_HOME/bin

2、  通过xftp将jar包上传上述目录

3、  导入jar到Oracle里

$loadjava -r -f -o -user oracle_userName/password@sid json-org.jar

4、  不使用时将jar删除

$dropjava -r -f -o -user oracle_userName/password@sid json-org.jar

5、  授权普通用户SocketPermission,已达到可以访问对应http接口

$sqlplus / as sysdb

$exec dbms_java.grant_permission( '数据库用户名', 'SYS:java.net.SocketPermission', 'IP:PORT-需要调用的http接口的IP及端口', 'connect,resolve' );

$ commit;

 

 

            

 

 

 

Oracle 用户增加java sources 的http工具类

createorreplaceandcompilejavasourcenamed httputils as

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.URL;

import java.net.HttpURLConnection;

import java.util.*;

import java.net.URLEncoder;

 

publicclass httpUtils

{

    /**

        urlPath:请求地址

       paramsJson:请求参数  json字符串格式

    **/

    publicstaticString sendRequestPostJson(String urlPath, String paramsJson) throws Exception{

         //发送post请求代码开始

        BufferedReader in = null;

        Stringresult = "";

        try {

            URL realUrl = newURL(urlPath);

            // 打开和URL之间的连接

            HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();

            // 设置通用的请求属性

            conn.setRequestProperty("accept", "application/json, text/javascript, */*; q=0.01");

            conn.setRequestProperty("Accept-Encoding", "gzip, deflate");

            conn.setRequestProperty("Connection", "keep-alive");

            conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");

            conn.setRequestProperty("Content-Type", "application/json");

           conn.setRequestProperty("Charset", "UTF-8");

            // 发送POST请求必须设置如下两行

            conn.setRequestMethod("POST");

           conn.setDoOutput(true);

            conn.setDoInput(true);

           conn.setUseCaches(false);

           // 往服务器里面发送数据

            if (paramsJson != null) {

                byte[] writebytes = paramsJson.getBytes("utf-8");

                // 设置文件长度

                conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));

                OutputStream outwritestream = conn.getOutputStream();

                outwritestream.write(writebytes);

                outwritestream.flush();

                outwritestream.close();

            }

            if (conn.getResponseCode() == 200) {

                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));

                result = in.readLine();

            }

        } catch (Exception e) {

            System.out.println("发送 POST 请求出现异常!"+e);

            e.printStackTrace();

            result=e.getMessage();

        }

        //使用finally块来关闭输出流、输入流

        finally{

            try{

                if(in!=null){

                    in.close();

                }

            } catch(IOException ex){

                ex.printStackTrace();

            }

        }

        returnresult;

    }

       /**

           urlPath:请求地址

           params:请求参数  name1=value1&name2=value2形式

       **/

    publicstaticString sendRequestPostBigData(String urlPath, String params) throws Exception{

         //发送post请求代码开始

        BufferedReader in = null;

        Stringresult = "";

        try {

            URL realUrl = newURL(urlPath);

            // 打开和URL之间的连接

            HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();

            // 设置通用的请求属性

            conn.setRequestProperty("accept", "application/json, text/javascript, */*; q=0.01");

            conn.setRequestProperty("Accept-Encoding", "gzip, deflate");

            conn.setRequestProperty("Connection", "keep-alive");

            conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");

            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

           conn.setRequestProperty("Charset", "UTF-8");

            // 发送POST请求必须设置如下两行

            conn.setRequestMethod("POST");

           conn.setDoOutput(true);

            conn.setDoInput(true);

           conn.setUseCaches(false);

           // 往服务器里面发送数据

            if (params != null) {

                byte[] writebytes = params.getBytes("utf-8");

                // 设置文件长度

                conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));

                OutputStream outwritestream = conn.getOutputStream();

                outwritestream.write(writebytes);

                outwritestream.flush();

                outwritestream.close();

            }

            if (conn.getResponseCode() == 200) {

                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));

                result = in.readLine();

            }

        } catch (Exception e) {

            System.out.println("发送 POST 请求出现异常!"+e);

            e.printStackTrace();

            result=e.getMessage();

        }

        //使用finally块来关闭输出流、输入流

        finally{

            try{

                if(in!=null){

                    in.close();

                }

            } catch(IOException ex){

                ex.printStackTrace();

            }

        }

        returnresult;

    }

 

}

  

 

 

           

 Oracle 用户增加java sources 的json解析工具类

  

createorreplaceandcompilejavasourcenamed jsonutilscustom as

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import java.lang.Integer;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

publicclass JsonUtilsCustom

{

    //取json串单个节点值

    publicstaticString getValue(String jsonStr,String nodeName){

       String nodeValue = "";

        Object node = null;

        try {

          if(jsonStr == null || !jsonStr.startsWith("{") || !jsonStr.endsWith("}")){

            nodeValue = "";

          }else{

            JSONObject obj = new JSONObject(jsonStr);

            node = obj.get(nodeName);

            if(node instanceof Integer) {

            nodeValue = String.valueOf(node);

           }

           if(node instanceof String) {

        nodeValue = String.valueOf(node);

      } 

          if(node instanceof JSONArray) {

            nodeValue = node.toString();

          }

          if(node instanceof JSONObject) {

            nodeValue = node.toString();

          }

        }

      } catch (JSONException e) {

        nodeValue = "";

      }

      return nodeValue;

  }

  //取json数组长度便于循环处理

  publicstaticInteger getArrayLength(String jsonArrayStr){

    Integerlength = 0;

    try {

      if(jsonArrayStr == null || !jsonArrayStr.startsWith("[") || !jsonArrayStr.endsWith("]")){

      length = -1;

      }else{

      JSONArray jsonArr = new JSONArray(jsonArrayStr);

      length = jsonArr.length();

      }

    } catch (JSONException e) {

      length = -1;

    }

    returnlength;

  }

 

  //取json数组第index个元素

  publicstaticString getArrayValue(String jsonStr,Integerindex){

    String nodeValue = "";

      Object node = null;

      try {

        if(jsonStr == null || !jsonStr.startsWith("[") || !jsonStr.endsWith("]")){

          nodeValue = "";

        }else{

          JSONArray jsonArr = new JSONArray(jsonStr);

          node = jsonArr.get(index);

          if(node instanceof Integer) {

            nodeValue = String.valueOf(node);

          }

          if(node instanceof String) {

            nodeValue = String.valueOf(node);

          }

         

          if(node instanceof JSONObject) {

            nodeValue = node.toString();

          }

         

          if(node instanceof JSONArray) {

            nodeValue = node.toString();

          }

        }

      } catch (JSONException e) {

        nodeValue = "";

      }

      return nodeValue;

  }

}

  

 

 

            

Oracle 用户创建function调用java

CREATEORREPLACEPACKAGE PKG_CUSTOM_UTILS IS

  --通过key获取json字符串中对应的值返回json字符串

  FUNCTION F_JSONGETVAL(JSONSTR VARCHAR2, NODENAME VARCHAR2) RETURNVARCHAR2;

    --通过索引获取jsonArray字符串对应数组中的值并返回json字符串

  FUNCTION F_JSONGETARRVAL(JSONARRAYSTR VARCHAR2, SEQNO NUMBER)

    RETURNVARCHAR2;

    --获取jsonArray字符串对应的数组长度

  FUNCTION F_JSONGETARRLEN(JSONARRAYSTR VARCHAR2) RETURNNUMBER;

    --http接口:入参为json字符串格式

  FUNCTION F_HTTPUTILS_JSON(URLPATH VARCHAR2, PARAMSJSON VARCHAR2) RETURNVARCHAR2;

    --http接口:入参为name1=value1&name2=value2格式字符串(直接调用共享交换平台对应的接口,header中添加Appkey

  FUNCTION F_HTTPUTILS_PARAMS(URLPATH VARCHAR2, PARAMS VARCHAR2) RETURNVARCHAR2;

 

END PKG_CUSTOM_UTILS;

 

 

CREATEORREPLACEPACKAGEBODY PKG_CUSTOM_UTILS IS

  FUNCTION F_JSONGETVAL(JSONSTR VARCHAR2, NODENAME VARCHAR2) RETURNVARCHAR2AS

    LANGUAGEJAVANAME'JsonUtilsCustom.getValue(java.lang.String,java.lang.String) return java.lang.String';

 

  FUNCTION F_JSONGETARRVAL(JSONARRAYSTR VARCHAR2, SEQNO NUMBER)

    RETURNVARCHAR2AS

    LANGUAGEJAVANAME'JsonUtilsCustom.getArrayValue(java.lang.String, java.lang.Integer) return java.lang.String';

 

  FUNCTION F_JSONGETARRLEN(JSONARRAYSTR VARCHAR2) RETURNNUMBERAS

    LANGUAGEJAVANAME'JsonUtilsCustom.getArrayLength(java.lang.String) return java.lang.Integer';

 

  FUNCTION F_HTTPUTILS_JSON(URLPATH VARCHAR2, PARAMSJSON VARCHAR2) RETURNVARCHAR2AS

    LANGUAGEJAVANAME'httpUtils.sendRequestPostJson(java.lang.String,java.lang.String) return java.lang.String';

 

  FUNCTION F_HTTPUTILS_PARAMS(URLPATH VARCHAR2, PARAMS VARCHAR2) RETURNVARCHAR2AS

    LANGUAGEJAVANAME'httpUtils.sendRequestPostBigData(java.lang.String,java.lang.String) return java.lang.String';

 

 

END PKG_CUSTOM_UTILS;

 

 

          

Oracle 测试调用http工具

SELECT PKG_CUSTOM_UTILS.F_HTTPUTILS('http://IP:PORT/path','{"a":"1","b":""}') AS resultValue FROM dual;

 

 

 

        

Oracle 测试调用json解析工具

 

SELECT PKG_CUSTOM_UTILS.F_JSONGETVAL('{"a":"1","b":""}','a') AS jsonValue FROM dual;

 









原文转载:http://www.shaoqun.com/a/512617.html

跨境电商:https://www.ikjzd.com/

二类电商:https://www.ikjzd.com/w/1457

naver:https://www.ikjzd.com/w/1727


授权及导入第三方jar1、登录root#su-oracle#cd$ORALE_HOME/bin2、通过xftp将jar包上传上述目录3、导入jar到Oracle里$loadjava-r-f-o-useroracle_userName/password@sidjson-org.jar4、不使用时将jar删除$dropjava-r-f-o-useroracle_userName/password@si
出口易:出口易
淘粉8:淘粉8
湖南局地最高气温回升至31℃ 这周末全省天气晴朗:湖南局地最高气温回升至31℃ 这周末全省天气晴朗
P卡爆出重大安全隐患:竟将卖家资金流水泄露给竞争对手:P卡爆出重大安全隐患:竟将卖家资金流水泄露给竞争对手
河源哪里有银杏林?河源赏银杏的好地方?:河源哪里有银杏林?河源赏银杏的好地方?

No comments:

Post a Comment