You always read that it is so easy to consume RESTful or SOAP web services in Apex. Well, yes, when you know how it works. It took me some time to figure it out, googling for bits of information on every step. If you are struggling too, this may help you to
get started.
The start lies in the database, and has nothing to do with Apex. You must make sure first that the web service you need can be reached from the database. For this, you have to configure a proper ACL (Access Control List). How to do this can be found in the
Apex Application Builder User's Guide,
Enabling Network Services in Oracle Database 11g. If you experience problems later on, read
this blog by Joel Kallman for a possible explanation why it doesn't work.
To check if you can call the web service, first call it from within the database, before going to Apex. To test if it really works in Apex, you should probably run this logged in as the Apex schema owner (e.g. APEX_040100 for apex 4.1). Since that is usually
not possible for developers, we will just assume (hope) it will work later on.
I have no idea what this web service at weather.gov does, but you can use it as a demo. It is just a good example of a GET type web service with 5 parameters.
In the simplest test, you call the url directly using utl_http.request, as you would in your browser:
- setscanoff
- selectutl_http.request('http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?lat=38.99&lon=-77.01&product=time-series&begin=2004-01-01T00:00:00&end=2013-04-20T00:00:00&maxt=maxt&mint=mint')weather
- fromdual;
Using Apex functionality, the URL, parameters and parameter values are split in different input parameters for procedure apex_web_service.make_rest_request:
- setscanoff
- declare
- v_resultclob;
- begin
- v_result:=apex_web_service.make_rest_request(
- p_url=>'http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php'
- ,p_http_method=>'GET'
- ,p_parm_name=>apex_util.string_to_table('lat:lon:product:maxt:mint')
- ,p_parm_value=>apex_util.string_to_table('39:-77:glance:maxt:mint')
- );
- dbms_output.put_line(v_result);
- end;
This should return a big xml document. Later on, in Apex, I will extract the maximum temperature from this xml document. The maximum temperature can be found in this node:
<temperature time-layout="k-p24h-n7-1" type="maximum" units="Fahrenheit">
<name>Daily Maximum Temperature</name>
<value>34</value>
... etc.
</temperature>
Now to Apex. I won't describe the basic steps that can be found in the User's Guide (like the fact that you create a web service reference in the Shared Components of the application).
You can use the url and parameters that you used when testing the web service in the database
The tricky part, for me anyway, was what to put in "Response XPath". This was new to me, so I had to take a crash course at w3schools.com
XPath.Tutorial.
This is where the XML response you got when testing the web service in the database comes in. In this example, the XPath is:
//temperature[@type="maximum"]/value
which (sort of) means: select all value elements that are children of temperature elements that have an attribute named type with a value 'maximum'.
The REST output parameter path is now just "/*", since the XPath already goes down to the value element.
So, here is a trick to play with the XPath. You copy the part of the XML response you need and put that part as text in this query:
- withcas
- (selectxmltype('<parametersapplicable-location="point1">
- <temperaturetime-layout="k-p24h-n7-1"type="maximum"units="Fahrenheit">
- <name>DailyMaximumTemperature</name>
- <value>50</value>
- <value>50</value>
- <value>62</value>
- <value>68</value>
- <value>61</value>
- <value>53</value>
- <value>52</value>
- </temperature>
- </parameters>')xmltype001
- fromdual
- )
- selectextractValue(value(t),'/*')"value"
- fromc
- ,table(xmlsequence(extract(c.xmltype001,'//temperature[@type="maximum"]/value')))t
Now you can easily play around with the Response XPath ('//temperature[@type="maximum"]/value') and Output parameter Path ('/*') until it works.
Once this is all done it is easy to create a page for this web service.
Create a new page and select Form, Form and Reports on Web Service. For the rest, you just follow the wizard.
There is another version of RESTful web services that doesn't use parameters (or it uses a mixed version). An example is this URL:
http://services.faa.gov/airport/status/SFO?format=xml
This uses the airport code ('SFO') as part of the URL, and format as a parameter. So, how do you put the airport code in the Apex web service reference? This is simply done by substituting a page item in the URL, e.g.
http://services.faa.gov/airport/status/&P5_AIRPORT_CODE.
In this case, you need to create a page item P5_AIRPORT_CODE manually. The wizard won't do this for you.
转载自:http://sqlcur.blogspot.sg/2013/02/apex-and-restful-web-services.html
分享到:
相关推荐
该文档说明c#端如何创建和调用RestFul接口,并给出详细的代码说明,方便用户学习和深入掌握。
Angular-RESTfulService, Angular 调用rest式服务的示例 Angular rest式服务示例这个项目简单地介绍了如何使用 Angular 来调用rest式服务。 只需克隆项目或者下载并提取. zip 即可开始。 Angular 概念使用app类和...
Restful C# 服务端篇之实现RestFul Service开发(简单实用)
.NET作为客户端,如何调用WEBAPI RESTFUL的服务端 .NET如何开发RESTFUL服务端
spring mvc restful service,支持string, json, xml, maven工程,可以直接运行
jersey RESTful service 源代码 jersey RESTful service 源代码
主要介绍了浅谈java调用Restful API接口的方式,具有一定借鉴价值,需要的朋友可以参考下。
主要介绍了vue 调用 RESTful风格接口操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
该文档说明c#端如何创建服务端RestFul Service接口,并给出详细的代码说明,方便用户学习和深入掌握。
大神的Restful Web Service 详细介绍,Objectives By the end of this class, you should be able to: Give a working definition of RESTful Web Services actions, Enable a RESTful user in the SM operator ...
本文讲述了什么是REST远程调用,已经在开发时如何做到符合REST规范的调用接口
使用RESTful Web Service实现转账业务,包括事务处理和并发控制
技術分享_RESTful Service with SSO Protection1
C++基于Restful web service ATM银行取款系统,实时视频播放和交易抓图的功能
NC65通过自带的rest文件检索restful接口,实现对外开放的restful接口,使用方式和其他web系统的使用方法一致,只是规范了访问链接格式
这是简单的一个在SAP中应用RESTful Web Service的例子
RESTful Web Service Primer RESTful Web Service Primer
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. FEATURES Powerful router ...Route parameters with wildcards and conditions Route redirect, halt, ...
Android Studio调用RestfulWCF接口
提供restful风格的框架, 拥有客户端和服务端两部分, 具体实现功能代码需要自己添加。