简体   繁体   English

REST URI设计:可选和多值信息传递

[英]REST URI Design : Optional and multivalue information passing

I have one search widget where people search for car dealers by zip code. 我有一个搜索小部件,人们可以通过邮政编码搜索汽车经销商。 There are also some optional checkboxes to refine search in that widget. 还有一些可选的复选框可以优化该小部件中的搜索。

Here is the URI for searching dealer by zip code. 这是用于按邮政编码搜索经销商的URI。

http://localhost:8080/dealer/zip/10080

If user selects checboxes then the URI will be 如果用户选择复选框,则URI为

http://localhost:8080/dealer/zip/10080servicetype=type1&servicetype=type2&servicetype=type3

I am using jersey . 我正在使用球衣 Here is java code. 这是Java代码。

@Path("/dealer")
@Produces(MediaType.APPLICATION_JSON)
public class DealerLocatorRS {
    private DealerService dealerService=new DealerService();

    @GET
    @Path("/zip/{zip}")
    public List<Dealer> getByZip(@PathParam("zip") String zip, 
        @QueryParam("servicetype") List<String> servicetype){
    .. . ..
    }

Is this right approach to pass optional and multiple values and . 这是传递可选和多个值的正确方法吗? Can anybody help me to apply best practices? 有人可以帮助我运用最佳做法吗?

I'm not sure that I'd map a search for dealers in a particular zip code to a resource; 我不确定是否将搜索到的特定邮政编码的经销商映射到资源。 it doesn't feel quite right. 感觉不太正确。 Instead, I'd have a resource that lists all the dealers, with individual dealers being sub-resources of that. 取而代之的是,我有一个列出所有经销商的资源,而每个经销商都是该经销商的子资源。 If it was possible to return a subset of the list of subresources restricted by properties (eg, their zip code) then that would be a great way to implement a search, otherwise I'd have a separate search handler that returns a list of links to matching dealer resources. 如果可以返回受属性(例如其邮政编码)限制的子资源列表的子集,那么这将是实现搜索的好方法,否则我将有一个单独的搜索处理程序来返回链接列表匹配经销商资源。

@Path("/dealer")
public class Dealers {
    @GET
    public List<Dealer> getAll() { ... }
    @GET
    @Path("search/byZip")
    public List<URI> getByZip(@QueryParam("zip") String zip, ...) { ... }
    @Path("{dealerId:[^/]+}")
    public Dealer getDealer(@PathParam("dealerId") String id) { ... }
}

If you are serious about understanding and applying REST, I'd recommend reading the REST paper , if you haven't done so yet. 如果您认真了解和应用REST,建议您阅读REST文章 ,如果您尚未这样做的话。

According to the architecture proposed in that paper, Each URL maps to a resource . 根据该论文提出的架构,每个URL都映射到一个资源 A resource could be something extrinsic and tangible, like a car dealership. 资源可能是外在的和有形的,例如汽车经销店。 Or it could be something "virtual" like a "region", or even a zipcode that might contain dealerships. 也可能是“虚拟”之类的东西,例如“区域”,甚至可能是包含经销商的邮政编码。

As to how you parameterize queries, think about what resource you want to use to satisfy or expose the queries. 关于如何参数化查询,请考虑要使用哪些资源来满足或公开查询。 Why would you treat "zipcode" as a variable parameter, any differently than, say your "servicetype"? 为什么将“邮政编码”作为变量参数,而不是说“服务类型”呢? Are they not both qualifiers to select a subset of dealerships? 他们不是两个都可以选择经销商的资格吗? Think about why you are making them different - there may be a good reason. 考虑一下为什么要使它们与众不同-可能有充分的理由。

For example, you could do: 例如,您可以这样做:

Think about the mapping of URLs to resources. 考虑一下URL到资源的映射。 It may be that two distinct URLs map to the same "result". 可能是两个不同的URL映射到相同的“结果”。 you need to decide whether that's appropriate for you. 您需要确定是否适合您。

It's also perfectly fine to retrieve a resource and then do queries on it on the client side. 检索资源然后在客户端对它进行查询也很好。 Not all work need be done by the server. 并非服务器需要完成所有工作。 You could search through the results obtained by http://server/dealer/zip/10070 on the client side, to find the ones that supply the desired services. 您可以搜索客户端http:// server / dealer / zip / 10070所获得的结果,以找到提供所需服务的结果。 This may or may not be a performance win, depending on the size of the data transmitted and the frequency and variety of queries. 这取决于性能,取决于传输的数据大小以及查询的频率和种类。

Supposing an overall result set of 10 (say, ten dealers within a zipcode), a Javascript foreach loop searching for a dealer that offers service X is going to be faster than an additional AJAX call asking the server to do that query on behalf of the client. 假设总结果集为10(例如,一个邮政编码中有10个经销商),则Java foreach循环搜索提供服务X的经销商将比另一个AJAX调用(要求服务器代表A做该查询)的速度更快。客户。

This is ok, unless your URL becomes too long (although URL length is not limited by any spec, some browsers and intermediaries limit it, so the best practices to keep it under 1K) 可以,除非您的URL太长(尽管URL长度不受任何规范限制,但某些浏览器和中介机构对此进行了限制,因此,最佳做法是将其保持在1K以内)

If it becomes too long, you may use POST instead of GET. 如果太长,可以使用POST而不是GET。

PS You have bug in your code, it should be @QueryParam("servicetype") List<String> servicetype) to match the example URI. PS您的代码中有错误,应该为@QueryParam("servicetype") List<String> servicetype)以匹配示例URI。

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

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