我有一个实体ListView
。
我的网址设计如下:
Temperature
我想一次创建多个温度(温度集合) - 是否有使用网址的约定?
到目前为止,我想出了以下内容:
GET /api/temperatures/new
GET /api/temperatures/{id}/edit
GET /api/temperatures
POST /api/temperatures
PUT /api/temperatures/{id}
DELETE /api/monitoring/temperatures/{id}
我认为必须有这样的约定,所以想跟你联系。
答案 0 :(得分:3)
GET /api/temperatures # Getting all resources
POST /api/temperatures # Create new resource
GET /api/temperatures/<id> # Get a single resource
PUT /api/temperatures/<id> # Edit all fields
PATCH /api/temperatures/<id> # Edit some fields
DELETE /api/temperatures/<id> # Delete a resource
这些是URL的Fielding在他关于REST的论文中描述的那种。您不应该描述URL中的终点,特别是在正确使用HTTP动词提供大量信息时。请注意,REST架构风格比JSON over HTTP更多。通用连接器,组件分离和无状态服务器是RESTful应用程序的关键组件。
注意:大多数人可能不会同时实现PUT和PATCH。 PUT会很好但我把它包括在内是为了完整性。
在回复您的评论时,如果您指的是使用一个POST请求创建多个资源,则不需要新的URL。您的应用程序应该能够在同一端点处理{temp: 45, date: ...}
和[{temp: 45, date: ...}, {temp: 50, date: ...}]
。
答案 1 :(得分:0)
HTTP方法GET
不适合创建或编辑资源 - /api/temperatures/new
和/api/temperatures/{id}/edit
。 HTTP GET
用于获取信息而不更改服务器中的状态。你应该use POST
or PUT
。
如果要创建多个温度,则应使用
POST /api/monitoring/temperatures
并使用JSON或XML对象列表。
Java示例:
@POST
@Path("/temperatures")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postTemperatures(Temperatures temperatures){
// process and save
}
@XmlRootElement
public class Temperatures {
public List<Temperature> temperatures;
Temperatures(){}
}
答案 2 :(得分:0)
您可以通过发送一系列温度而不是单个条目来使用单个帖子更新多个条目,
POST /api/temperatures { [{...},{...}] }
但您的api端点结构可以简化一点。
理想情况下,您需要为所有API资源提供简单一致的界面。
我个人会简化:
GET /api/temperatures/new
GET /api/temperatures/{id}/edit
GET /api/temperatures
POST /api/temperatures
PUT /api/temperatures/{id}
DELETE /api/monitoring/temperatures/{id}
到
GET /api/temperatures // Get all temperatures
POST /api/temperatures // Send in array of new entries to update
GET /api/temperatures/{id} // Read a specific temperature
PUT /api/temperatures/{id} // Update a specific temperature
DELETE /api/temperatures/{id} // Delete a specific temperature
这为映射到CRUD接口的所有温度相关调用提供了一致的api接口。
如果没有上下文,很难确定/ api / temperature / new的用途,但我会考虑在调用中使用一个参数来细化响应。
e.g。
/api/temperatures?age=new // Get new temps
这将允许您稍后使用通用结构添加不同类型的条件。