Oracle Rest API多行
我必须创建一种GET类型的API,并且必须连接两个表。
例如,我有以下表:
表1:
- customer_id:{001}
- first_name:{f_name}
- last_name:{l:name}
表2:
- customer_id:{001} {001} {001}
- 街道:
- zip_code:{1234} {1234}
- 城市:{xxxx} {xxxx}
- country:{xx} {xx} {xx}
如果我连接两个表,我会得到以下结果:
{
"customer_id": 001,
"first_name": "f_name",
"last_name": "l_name",
"street": "A street",
"zip_code": "1234",
"city": "xxxx",
"country": xx
}
{
"customer_id": 001,
"first_name": "f_name",
"last_name": "l_name",
"street": "B street",
"zip_code": "1234",
"city": "xxxx",
"country": xx
}
这是beacuse table2 thable thable the table2 cubtist2 cubtistion_id:001 ”。
但是我希望这种结果:
{
"customer_id": 001,
"first_name": "f_name",
"last_name": "l_name",
"address": [
{
"street": "A steet",
"zip_code": "1234",
"city": "xxxx",
"country": xx
},
{
"street": "B street",
"zip_code": "1234",
"city": "xxxx",
"country": xx
}
]
}
似乎一个简单的查询在这里无法使用。是否有人理想我应该如何创建这种GET类型的API?
I have to create a GET type of API and I have to connect two tables.
For example, I have the following tables:
Table 1:
- customer_id: {001}
- first_name: {f_name}
- last_name: {l:name}
Table 2:
- customer_id: {001} {001}
- street: {A street} {B street}
- zip_code: {1234} {1234}
- city: {xxxx} {xxxx}
- country: {xx} {xx}
If I connect the two tables I get the following result:
{
"customer_id": 001,
"first_name": "f_name",
"last_name": "l_name",
"street": "A street",
"zip_code": "1234",
"city": "xxxx",
"country": xx
}
{
"customer_id": 001,
"first_name": "f_name",
"last_name": "l_name",
"street": "B street",
"zip_code": "1234",
"city": "xxxx",
"country": xx
}
This is beacuse the table2 has two rows with the customer_id:"001".
But I want this kind of result:
{
"customer_id": 001,
"first_name": "f_name",
"last_name": "l_name",
"address": [
{
"street": "A steet",
"zip_code": "1234",
"city": "xxxx",
"country": xx
},
{
"street": "B street",
"zip_code": "1234",
"city": "xxxx",
"country": xx
}
]
}
It seems like a simple query won't work here. Has anybody ideal how should I create this GET type of API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们将在查询中使用 CURSOR。
首先是表和数据:
然后我们可以构建 REST API。
我们的模板是 cust_address/:id,我们将对其执行 GET 操作。
GET 处理程序背后的 SQL:
将所有这些放在一起,我们的 TEMPLATE/HANDLER 组合在 ORDS 中定义如下(如 SQL Developer Web 中所示)
然后我们调用 API - 就在我们的浏览器:
您需要关闭集合查询的分页(设置为 0),或者需要将处理程序定义为集合项。
免责声明:我是 Oracle 员工,也是 Oracle REST 数据服务的产品经理。
We're going to use a CURSOR in the query.
First the tables and data:
Then we can build the REST API.
Our template is cust_address/:id, and we're going to do a GET on that.
The SQL behind the GET handler:
Putting that all together, our TEMPLATE/HANDLER combo is defined like this in ORDS (as shown in SQL Developer Web)
Then we call the API - just in our browser:
You'll need to either turn paging off for a Collection Query (set to 0), or you'll need to define your handler as a Collection Item.
Disclaimer: I'm an Oracle employee and the product manager for Oracle REST Data Services.
这可以使用 GET API 的简单
SELECT
语句来创建。您需要使用CURSOR
命令在 JSON 对象中创建嵌套数组。下面是您需要的示例。下面示例中的 :p_customer_id 是来自 API 的某种输入参数。
This can be created using a simple
SELECT
statement for your GET API. You need to use theCURSOR
command to create nested arrays within your JSON object. An example of what you need can be seen below.:p_customer_id in the example below is some sort of input parameter from your API.