简体   繁体   English

什么是设计DELETE以获得宁静资源的最佳方式

[英]Whats the best way to design DELETE for a restful resource

Trying to find out what is a good way to design a delete. 试图找出设计删除的好方法。

trying to explain using example: 试图用例子解释:

I have a resource for Employee. 我有一个Employee资源。 The employee has the following fields 该员工具有以下字段

Employee{
    String firstName
    String lastName
    String emailId
    String managerEmployeeId
    String employeeId
}

I want the ability to delete the employee using any of the fields eg firstName , lastName , employeeId , or managerEmployeeId 我希望能够使用任何字段删除员工,例如firstName,lastName,employeeId或managerEmployeeId

How should my resource paths look ideally 我的资源路径应该如何理想

Option1: is this a good option ? 选项1:这是一个不错的选择吗? yes ? 是吗? no ? 不? why ? 为什么?

/employee/id/{employeeId}
/employee/firstName/{firstName}
/employee/lastName/{lastName}
/employee/managerId/{managerId}

Option2: 选项2:

use of query params 使用查询参数

    /employee?id=100  ( to delete employee with id 100)

    /employee?firstName=Tom&lastName=Winn (to delete an employee named Tom Winn)

   /employee?managerEmployeeId=400 (delete all employees having manager id as 400)

Option 1 looks very RPC-ish to me 选项1看起来非常像RPC-ish

I like the second option but I find it very error prone since the fields have to be specified. 我喜欢第二个选项,但我发现它非常容易出错,因为必须指定字段。

In the 2nd option I don't like the idea of having a param named firstName and then mapping it to a firstName field in a Java class Employee. 在第二个选项中,我不喜欢有一个名为firstName的参数,然后将其映射到Java类Employee中的firstName字段。 Is this a used paradigm in the industry ? 这是行业中使用的范例吗?

Further this is for a backend application with xml and written in java( use of loose field names looks very un-java to me) 进一步这是用于xml的后端应用程序并用java编写(使用松散字段名称对我来说非常非java)

I would like to understand what is being followed in the industry ( specifically for a strongly typed java+xml based rest systems built in jersey , restEasy or Spring ) 我想了解业界正在遵循的内容(特别是基于jersey,restEasy或Spring构建的强类型java + xml基础系统)

My vote would go for (a slight alteration of) option 1. 我的选票将用于(略微改动)选项1。

DELETE /employees/{employeeId}

There's no need to specify /id , I can't imagine there's ever going to be any confusion (unless you have an employee named some bizarre UUID ...). 没有必要指定/id ,我无法想象会有任何混淆(除非你有一个员工命名一些离奇的UUID ......)。 This also leads me to why I'd say the others were a bad idea: 这也让我想到为什么我会说其他人是个主意:

There should only ever be one Employee with that id , so there is no room for ambiguity. 应该只有一个具有该id Employee ,因此没有歧义的余地。 There are likely to be many with the same firstName , lastName (and even combinations may not be unique). 可能有许多具有相同firstNamelastName (甚至组合可能不是唯一的)。

If you put /id in the string, it implies (to me at least) that there may be some other way to uniquely refer to that resource (eg the /firstName or /lastName which you suggest); 如果你把/id放在字符串中,它意味着(至少对我来说)可能有其他方式来唯一地引用该资源(例如你建议的/firstName/lastName ); omitting it leaves it clear that's the only way you support referencing a specific Employee , and that if they want to find the one with a specific firstName and lastName , it should be retrieved with a GET /employees?firstName=Tom&lastName=Winn type request. 省略它是明确的,这是你支持引用特定Employee唯一方法,如果他们想要找到具有特定firstNamelastName那个,那么应该使用GET /employees?firstName=Tom&lastName=Winn检索它GET /employees?firstName=Tom&lastName=Winn type request。

Query parameters in a DELETE sound risky to me, and more like something whatever is calling your service should be in charge of. 查询DELETE声音中的参数对我来说是有风险的,更像是任何调用您的服务的东西都应该负责。 If you want to DELETE all Employee s with managerId=400 , it makes more sense to GET those resources: GET /employees?managerId=400 , and then issue a DELETE on all the returned Employee s. 如果要DELETE所有使用managerId=400 Employee ,那么GET这些资源更有意义: GET /employees?managerId=400 ,然后对所有返回的Employee发出DELETE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM