ORACLE stored procedure calls Web Service

1 Overview

In the ESB project   recently , most of the customer's service calls between various systems are carried out in the oracle stored procedure. This article describes the oracle stored procedure calling web service . Other mainstream databases, such as mysql and sql service , will not introduce the method of calling web service here. This article is mainly used to introduce the method of oracle stored procedure calling web service .

  As we all know, when a Web Service sends a request and receives a result through the HTTP protocol, the sent request content and result content are encapsulated in XML format, and some specific HTTP message headers are added to describe the content format of the HTTP message . The message header and XML content format is the SOAP protocol. The SOAP protocol is based on the HTTP protocol. The relationship between the two is like the highway is based on the transformation of the ordinary highway. After adding a fence to a highway, it becomes a highway.

  In the same way, in the stored procedure of Oracle , can the Web Service be called by creating a message in XML format + HTTP protocol ? The answer is yes, there is a toolkit called UTL_HTTP in ORACLE , we can use this toolkit to implement stored procedure call Web Service .

The AEAI ESB  mentioned above is one of the core products of Shutong Changlian, which can realize functions such as WEB service development and WEB service registration. The interface example in this article is considered to use the WEB service created by ESB , but due to the needs involved The content of the introduction is too far from the topic of this article, so I use the Web service sample that comes with the AEAI DP development platform to illustrate. Readers who are interested in AEAI ESB can find out through the relevant links at the end of this article.

2. Intended audience

  • New employees of Shutong Changlian
  • tech enthusiasts

3. Environmental Information

Operating System: Windows7

Oracle:  version is oracle11g

Mysql : The version is mysql5.1

Jdk  jdk1.6.0_10

4. Glossary

AEAI ESB : The application integration platform is mainly used as the " keel " of the enterprise information system to integrate various business systems. It is generally called the Enterprise Service BUS ( ESB ). AEAI ESB .

AEAI DP : AEAI DP application development platform is specially used to developMIS Java Web applications, also known as Miscdp ( Misc Develope Platform ) comprehensive application development platform.  The AEAI DP application development platform is also used as a support tool for extended development in the Shutong Changlian software product family, such as: extended development of Portlet components, Web Service and Http Service for the AEAI Portal portal platform; extended development of business process forms for the AEAI BPM process integration platform and functions, etc.

Stored procedure : In a large database system, a set of SQL  statements to complete a specific function is stored in the database. After the first compilation, calling it again does not require recompilation. The user specifies the name of the stored procedure and gives parameters (if the stored procedure with parameters) to execute it.

UTL_HTTP : The HTTP protocol toolkitthat comes with oracle can be used to send post requests.

PL/SQL Developer: An integrated development environment , developed by Allround Automations , specifically forthe development of program units stored in the Oracle database

5. Operation steps

5.1 Create a sample interface

  Use the AEAI DP development platform to create an application with its own WS service, as shown below:

 

  Select database information

  After deploying the application, check the WS service that comes with the application you just created

 

5.2 Creating Stored Procedures

5.2.1 Basic syntax

  The following is the basic syntax for creating a stored procedure

CREATE OR REPLACE PROCEDURE stored procedure name  ( --definition parameter  )

IS

define variable

BEGIN

Start PL/SQL body

END

Indicates the end of the PL/SQL body

5.2.2 Creation steps

1. Open PL/SQL and open a sql window

 2. Put the statement that creates the stored procedure into it and execute it

 

  Such a stored procedure sample calling web service is created, the following is the detailed sample sql body

--Create a stored procedure, define four parameters, input parameters: userid, code, name; output parameters: resmark

CREATE OR REPLACE PROCEDURE pro_test_ws(name in varchar2,resmark out varchar2) IS

--Define four variables, http request, http return, request message, return message

  http_req    UTL_HTTP.REQ;

  http_Resp   UTL_HTTP.RESP;

  request_env VARCHAR2(32767);

  l_Replyline VARCHAR2(1000);

BEGIN

--start pl/sql body

  request_env := '

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:demo="http://demo.service.wstest.agileai.com/">

   <soapenv:Header/>

   <soapenv:Body>

      <demo:sayHi>

         <!--Optional:-->

         <theGirlName>'|| name ||'</theGirlName>

      </demo:sayHi>

   </soapenv:Body>

</soapenv:Envelope>

';

--打印请求报文

dbms_output.put_line(request_env);

--请求WS地址

  http_req    := UTL_HTTP.

                 begin_request('http://localhost:6060/cam/services/UserSync?wsdl',

                               'POST',

                               UTL_HTTP.http_version_1_1);

-- 保持连接状态

      Utl_Http.Set_Persistent_Conn_Support(http_req, TRUE);

--设置编码

      Utl_Http.Set_Header(http_req, 'Content-Type', 'text/xml;charset=utf-8');

      

      Utl_Http.Set_Header(http_req, 'SOAPAction', '');

--设置字符集

      Utl_Http.Set_Body_Charset(http_req, 'utf-8');

--该参数代表我发送的POST报文多长,不可少

      Utl_Http.Set_Header(http_req, 'Content-Length', Lengthb(request_env));

      Utl_Http.Write_Line(http_req, request_env);

--赋值http返回

      http_Resp := Utl_Http.Get_Response(http_req);

--将请求报文赋值给 l_Replyline

      Utl_Http.Read_Text(http_Resp, l_Replyline);

      dbms_output.put_line(l_Replyline);

--付给存储过程出参

resmark:=l_Replyline;

END pro_test_ws;

5.2.3 关键点说明

  在存储过程中,使用UTL_HTTP工具包调用web服务时,几个关键方法的使用说明

1. 通过设置请求地址、方式、协议版本,得到http请求对象

http_req    := UTL_HTTP.

                 begin_request(' http://localhost:6060/wstest_project/services/HelloWorld?wsdl ',

                               'POST',

                               UTL_HTTP.http_version_1_1);

2. 设置协议保持连接状态

Utl_Http.Set_Persistent_Conn_Support(http_req, TRUE);

3. 设置请求编码,SOAPAction header的值为空串("")表示SOAP消息的目的地由HTTP请求的URI标识;无值则表示没有指定这条消息的目的地。

Utl_Http.Set_Header(http_req, 'Content-Type', 'text/xml;charset=utf-8');

Utl_Http.Set_Header(http_req, 'SOAPAction', '');

4. 设置字符集

Utl_Http.Set_Body_Charset(http_req, 'utf-8');

5. 报文长度

Utl_Http.Set_Header(http_req, 'Content-Length', Lengthb(request_env));

6. 调用服务,发送报文

Utl_Http.Write_Line(http_req, request_env);

7. 得到返回体

http_Resp := Utl_Http.Get_Response(http_req);

8. 将返回报文赋值给变量

Utl_Http.Read_Text(http_Resp, l_Replyline);

5.3 调用存储过程

5.3.1 使用PL/SQL Developer测试

1) 选中存储过程的名字,右键选择测试,进入测试页面

 

2) 添加响应的参数值,F9或者点击按钮开始执行,执行后可以得到看到返回值

 

3) 切换到DBMS输出页面,可以看到打印的内容

5.3.2 使用sql代码调用

DECLARE

resmark varchar2(1000);

BEGIN

  pro_test_ws(''小郑',resmark);

  DBMS_OUTPUT.PUT_LINE(resmark);

END;

1) 打开sql窗口,执行上面的sql语句

2) 查看输出信息

  1处为存储过程打印的信息,2为调用时打印输出的信息

6. 总结说明

  本文介绍了在oracle存储过程中,使用UTL_HTTP工具包,通过创建请求报文以及使用HTTP协议来调用Web Service,从创建oracle存储过程以及UTL_HTTP相关参数的配置,到通过PL/SQL Developer测试调用以及sql代码进行调用来详细说明。

  附件为存储过程创建sql、调用sql以及接口程序和相关的数据库文件。

7. 相关链接

AEAI DP开发平台/ AEAI ESB集成平台相关介质以及文档资料地址:http://www.agileai.com/portal/website/01/res-share.ptml

 

文档及代码附件下载http://pan.baidu.com/s/1kVyMVQn

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326522996&siteId=291194637