可以对get和post rest api使用相同的资源名称吗 [英] Is it okay to use same resource name for both get and post rest api

查看:142
本文介绍了可以对get和post rest api使用相同的资源名称吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我只用1个GET资源就用Java开发了Restful服务.像这样被访问:

Sometime back I developed a Restful service in Java with only 1 GET resource. It was accessed like this:

获取 http://localhost:8080/my-project/customers/transactions

此GET请求返回所有客户交易.

This GET request returns all the customer transactions.

现在,我有另一个项目请求,他们想要在同一数据库的不同架构中插入客户交易.我认为与其创建其他服务,不如可以增强此服务,因为基础数据库是相同的,并且与客户交易有关.

Now, I have another project request where they want to insert customer transactions in a different schema in same database. I thought instead of creating other service I could enhance this service since underlying database is same and it's about customer transactions.

因此,我在服务接口createCustomerTransactions中创建了另一种方法,并且我想将其命名为与GET请求相同的名称,但此方法将是POST,如下所示:

So, I created another method in my service interface createCustomerTransactions and I am thinking to name it same as my GET request but this one will be POST like this:

POST http://localhost:8080/my-project/customers/transactions

我使用Soap-UI对此进行了测试,并且可以正常工作.我的问题是做Restful的正确方法.虽然内部将GET和POST指向不同的实际方法,但它们具有相同的URL是否可以? 我的名字不好,所以不能为资源再命名一个更好的名字.

I tested this using Soap-UI and it works. My question is it the right way to do Restful. Is it okay to have both GET and POST having same url, internally though they will be pointing to different actual methods? I am not good with names so can't come up another better name for resource.

推荐答案

TL; DR是的,这实际上是一个好习惯.

TL;DR Yes you can, it is actually a good practice.

这是原因:

与HTTP一起使用时,最好的时机取决于资源(URL),并依赖于HTTP动词的动作,常见且良好的做法是使用这些动词来识别对您拥有的资源进行的某些操作:

Restful when is used with HTTP depends in the resources(URL's) and rely the actions the HTTP verbs, it is common and good practice used this verbs to identify some operations over the resources you have:

  • GET检索全部或仅一个资源.
  • POST通常用于创建新资源.
  • 用于更新资源的PUT
  • 删除删除资源

  • GET retrieve all or just one resource.
  • POST is normally for create a new resource.
  • PUT used to update a resource
  • DELETE delete a resource

在启动Restful API之前,我们应该做的首要任务之一就是确定哪些是我们需要的资源,以及它们的属性是什么.这种方法的第一个规则是使用名词而不是动词,例如person,ticket,customer等.

One of the first tasks that we should do before starting our Restful API is identify which are the resources that we need to have and what will be the attributes of them. The first rule over this approach is to use nouns not verbs, such as person, ticket, customer , etc..

定义了资源后,您需要确定哪些操作适用于它们,以及这些操作将如何映射到您的API. RESTful原则提供了使用映射如下的HTTP方法来处理CRUD操作的策略.

Once you have your resources defined, you need to identify what actions apply to them and how those would map to your API. RESTful principles provide strategies to handle CRUD actions using HTTP methods mapped as follows.

获取/tickets-检索门票列表

GET /tickets - Retrieves a list of tickets

获取/tickets/12-检索特定票证

GET /tickets/12 - Retrieves a specific ticket

POST/票证-创建新票证

POST /tickets - Creates a new ticket

PUT/tickets/12-更新故障单#12

PUT /tickets/12 - Updates ticket #12

PATCH/tickets/12-部分更新票证#12<-检查此方法.

PATCH /tickets/12 - Partially updates ticket #12 <-- check this approach.

删除/tickets/12-删除第12号门票

DELETE /tickets/12 - Deletes ticket #12

以上内容取决于防火墙的配置,但可以将以上内容视为对API设计原则的建议.

The above depends on firewall configurations, but consider the above as a suggestion for API design principles.

这篇关于可以对get和post rest api使用相同的资源名称吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆