gpt4 book ai didi

web-services - 将 restful 架构映射到 CRUD 功能

转载 作者:行者123 更新时间:2023-12-02 04:00:49 24 4
gpt4 key购买 nike

我正在尝试用 Java 开发我的第一个 Restful 服务,并且在将方法映射到 CRUD 功能时遇到了一些麻烦。
我的 uri 结构如下并映射到基本数据库结构:

/databases/{schema}/{table}/

/databases is static

{schema} and {table} are dynamic and react upon the path parameter


这就是我所拥有的:
Method - URI        - DATA      - Comment
---------------------------------------------------------------------
GET - /databases - none - returns a list of databases
POST - /databases - database1 - creates a database named database1
DELETE - /databases - database1 - deletes the database1 database
PUT - /databases - daatbase1 - updates database1
目前在上面的示例中,我将数据库名称作为 JSON 对象传递。但是,我不确定这是否正确。我是否应该这样做(以 DELETE 方法为例):
Method - URI                  - DATA - Comment
---------------------------------------------------------------------
DELETE - /databases/database1 - none - deletes the database with the same name
如果这是正确的方法并且我需要传递额外的数据,那么下面的内容是否正确:
Method - URI                  - DATA      - Comment
---------------------------------------------------------------------
DELETE - /databases/database1 - some data - deletes the database with the same name
任何意见将不胜感激

最佳答案

REST 是进入您的域的接口(interface)。因此,如果您想 公开数据库 那么 CRUD 可能会起作用。但是 REST 还有更多功能(见下文)

REST-afarians 会反对您的服务是 RESTful,因为如果不符合关键约束之一:超媒体约束 .但是,如果您添加 ,则可以解决此问题。友情链接 到您的服务将生成/提供的文档(超媒体)。见Hypermedia constrain .在此之后,您的用户将按照链接和表单来更改应用程序中的内容。 (您的示例中的数据库、表和行):

- GET /database -> List of databases
- GET /database/{name} -> List of tables
- GET /database/{name}/{table}?page=1 -> First set of rows in table XXXXX
- POST /database/{name}/{table} -> Create a record
- PUT /database/{name}/{table}/{PK} -> Update a record
- DELETE /database/{name}/{table}/{PK} -> Send the record to the big PC in the sky..

不要忘记为您的文档添加链接!

将 REST 用于 CRUD 有点像将其放入 Straitjacket :) : 你的 URI 可以代表任何概念。因此,如何尝试根据您希望您的服务或 Web 应用程序执行的底层资源(功能)公开一些更有创意/更丰富的 URI。

看看这篇很棒的文章: How to GET a Cup of Coffee

关于web-services - 将 restful 架构映射到 CRUD 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10603271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com