简体   繁体   English

RESTful服务-如何设计具有许多参数的URL

[英]RESTful services - how to design a URL with many parameters

I've developed REST services, but now I realized that I'm doing something wrong. 我已经开发了REST服务,但是现在我意识到自己在做错事。

For example, I have a service which retrieves information about a specific device. 例如,我有一个检索有关特定设备信息的服务。 Each device has an address: sector.room.group.id . 每个设备都有一个地址: ector.room.group.id The URI I did for this GET method was: (...)/services_devices/{sector}/{room}/{group}/{id} But now I realized that I should not have used the '/' to separate the device address, right? 我为此GET方法执行的URI是: (...)/services_devices/{sector}/{room}/{group}/{id}但现在我意识到我不应该使用'/'来分隔设备地址,对不对? How should I pass the address to this method? 我应该如何将地址传递给此方法? Using ';' 使用';' ?

My GET method is: 我的GET方法是:

@GET
@Path("{sector}/{room}/{group}/{id}")
@Produces("application/json")
public String getDeviceName(@PathParam("sector") int sector, @PathParam("room") int room, @PathParam("group") int group, @PathParam("id") int id) throws Exception
{
    String name = null;

    try {
            name = new DevicesManager().getDeviceName(sector, room, group, id); 
    } catch (Exception e) {
            e.printStackTrace();
    }
    return name;
}

There is a simple way of change this, to have a correct URI? 有一种简单的更改方法,可以使用正确的URI? I have this "error" in many methods. 我在许多方法中都有这个“错误”。

If there is a hierarchy in your resources path variables are appropriate. 如果您的资源中存在层次结构,则路径变量是适当的。

It seems in your case there is a hierarchy between devices and address, but first comes the address and after the deviceName. 在您的情况下,设备和地址之间似乎存在层次结构,但是首先出现在地址之后,在deviceName之后。 "deviceName" can be considered a one more hierarchy step. 可以将“ deviceName”视为另一个层次结构步骤。

The best way to reflect the above relations would be the following url: 反映上述关系的最佳方法是以下网址:

(...)/sector/room/group/id/deviceName (...)/扇区/房间/组/ ID /设备名称

You can then have another attribute of the device mapped like this: 然后,您可以像下面这样映射设备的另一个属性:

(...)/sector/room/group/id/deviceOwner (...)/部门/房间/组/ ID /设备所有者

The JAX-RS mapping would be: JAX-RS映射将是:

@GET
@Path("{sector}/{room}/{group}/{id}/deviceName")
@Produces("application/json")
public String getDeviceName(@PathParam ...) {
//impl.
}

And yes, if the deviceName is the only relevant attribute of the resource, then you can leave out "deviceName" and your orignal mapping is correct. 是的,如果deviceName是资源的唯一相关属性,则可以省略“ deviceName”,并且原始映射正确。

If the resource at /sector/room/group/id has many attributes you should consider returning a composed object for the path: 如果/sector/room/group/id具有许多属性,则应考虑返回路径的组合对象:

@GET
@Path("{sector}/{room}/{group}/{id}")
@Produces("application/json")
public Device getDeviceName(@PathParam...) {
}

REST architectural style introduces HATEOAS, which means that client and server are loosely coupled. REST体系结构风格引入了HATEOAS,这意味着客户端和服务器之间是松散耦合的。 Simply the client is not aware of how the URLs look like and gets them from previous responses. 只是客户不知道URL的样子,并无法从先前的响应中获取它们。 (it's similar like surfing thru HTML pages). (类似于通过HTML页面浏览)。 Of course there will be at least one URL, an entry point, that is known to the client. 当然,将至少有一个客户端知道的URL,即入口点。 From this point of view, your need to have correct URIs is irrelevant. 从这个角度来看,您是否需要正确的URI是无关紧要的。 What's correct URI? 什么是正确的URI? The URI is correct when its form is aligned with RFC. URI与RFC对齐时正确。

You are probably introducing URL patterns, that are not RESTful, because it implicates tight coupling between client and server (the client must be aware of URL patterns and have ability to construct URLs from them; fill up sector/room/ etc. in your case) 您可能正在引入不是RESTful的URL模式,因为它暗示了客户端和服务器之间的紧密耦合(客户端必须了解URL模式并具有从中构造URL的能力;根据您的情况填充扇区/房间/等)。 )

See also this post: 另请参阅此帖子:

http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven http://roy.gbiv.com/untangled/2008/rest-apis-must-be-超文本驱动

My advice is; 我的建议是; don't waste your time on URL patterns, make URLs simple as is possible, flat hierarchy has also many benefits, and follow HATEOAS principle. 不要浪费时间在URL模式上,要使URL尽可能简单,平面层次结构也有很多好处,并遵循HATEOAS原则。

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

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