2016-05-25 39 views
3

我有一个实体Temperature用于收集对象的RESTful URL

我的网址设计如下:

GET  /api/temperatures/new 
GET  /api/temperatures/{id}/edit 
GET  /api/temperatures 
POST  /api/temperatures 
PUT  /api/temperatures/{id} 
DELETE /api/monitoring/temperatures/{id} 

我想一次建立多个温度(温度的集合) - 是那里的网址使用方面进行任何约定?

到目前为止,我想出了以下内容:

POST /api/monitoring/temperatures/collection 
GET /api/monitoring/temperatures/cnew 

我认为必须有这样的惯例已经所以想查你。

+0

为什么不使用'POST'将温度数组发送到'/ api/temperature'端点? –

回答

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(){} 
} 
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的菲尔丁的描述在他的论文REST。您不应该描述URL中的终结点,特别是在正确使用时,HTTP动词提供大量信息。请注意,REST架构风格比JSON over HTTP更重要。通用连接器,组件和无状态服务器的分离是RESTful应用程序的关键组件。

注:大多数人可能不会实现PUT和PATCH。 PUT将会很好,但为了完整性我将它包含在内。

作为对您的评论的回应,如果您指的是使用一个POST请求创建多个资源,则不需要新的URL。您的应用程序应该能够在同一端点处理{temp: 45, date: ...}[{temp: 45, date: ...}, {temp: 50, date: ...}]

+0

谢谢@IanAuld你有什么建议吗? – Mick

+0

建议做什么?如果您需要创建多个新资源,则需要发送多个POST或能够处理包含新资源数组的POST请求。 – IanAuld

+0

太棒了 - 现在你会在第二个场景中发布什么网址? – Mick

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 

这给出了一个一致的接口的API映射到CRUD接口的所有温度相关的调用。

如果没有上下文,很难确切知道/ api/temperature/new的用法,但我会考虑在调用中使用参数来细化响应。

例如

/api/temperatures?age=new  // Get new temps 

这将允许您使用通用结构以后添加不同类型的标准。