简体   繁体   English

如何将REST请求的参数传递给Java方法?

[英]How to pass parameters for REST request to Java method?

We are building a Java SDK to simplify the access to one of our services that provide a REST API. 我们正在构建Java SDK以简化对提供REST API的服务的访问。 This SDK is to be used by 3rd-party developers. 此SDK将由第三方开发人员使用。

One of the problems has been to find a good way to represent the optional parameters of each request. 其中一个问题是找到一种表示每个请求的可选参数的好方法。

Let's say we have the rest endpoint: GET /photos which has several optional parameters: sortBy , pageSize , pageNumber , etc. 假设我们有其余的端点: GET /photos ,它有几个可选参数: sortBypageSizepageNumber等。

One solution would be to accept a Map<String,String> as a parameter to the api method that represents that rest call. 一种解决方案是接受Map<String,String>作为表示该休息调用的api方法的参数。

interface RestService {   
    public List<Photo> getPhotos(Map<String,String> parameters);
}

There are a couple of problems with this solution: 这个解决方案有几个问题:

  • The method's signature gives the developer no information about the names of optional parameters available to this method (as well as the valid values). 该方法的签名不向开发人员提供有关此方法可用的可选参数名称的信息(以及有效值)。 He would need to consult the REST api documentation, and ideally we would like to avoid the need to. 他需要查阅REST api文档,理想情况下我们希望避免使用。
  • The developer will end up creating several Map's with the parameters for the methods he will call, hard coding the names and values of the parameters everywhere. 开发人员最终会创建几个Map,其中包含他将调用的方法的参数,硬编码参数的名称和值。 This means that if they ever change, it will be an hassle to fix them. 这意味着如果它们发生变化,修复它们将是一件麻烦事。

Another solution would be to use an Options object that would contain information about the optional parameters. 另一种解决方案是使用包含有关可选参数的信息的Options对象。 We could even use the builder pattern to build this object. 我们甚至可以使用构建器模式来构建此对象。

This is how the method signature would look like: 这是方法签名的样子:

interface RestService {   
    public List<Photo> getPhotos(PhotosOptions options);
}

The developer now knows what are the available optional parameters and can easily build a PhotosOptions like this: 开发人员现在知道可用的可选参数是什么,并且可以轻松地构建如下的PhotosOptions

PhotosOptions options = PhotosOptions.builder().sortBy(DESC).pageSize(200).build();
List<Photo> photos = getPhotos(options);

The problem is that the service we are trying to cover with the SDK has a huge range of requests that we need to implement and pretty much all of them have a different list of options that they allow. 问题是我们试图用SDK覆盖的服务有很多我们需要实现的请求,而且几乎所有这些请求都有不同的选项列表。 This may result in a large number of Options classes. 这可能会导致大量的Options类。

What is the best solution here? 这里最好的解决方案是什么? Build an Options object (and its builder) for each combination? 为每个组合构建一个Options对象(及其构建器)? Inheritance doesn't quite help here because I have all sorts of combinations of parameters. 继承在这里没有多大帮助,因为我有各种各样的参数组合。

The better way is use options object as you showed: 更好的方法是使用选项对象,如您所示:

interface RestService {   
    public List<Photo> getPhotos(PhotosOptions options);
}

This is very difficult method and require to type a lot of letters, you will have a lot of classes and other ass-pain. 这是非常困难的方法,需要输入很多字母,你会有很多课程和其他麻烦。

But my opinion is that this way is better then others because it provides a strong methods signature, makes your SDK more deterministic. 但我的观点是,这种方式比其他方式更好,因为它提供了强大的方法签名,使您的SDK更具确定性。 If you separate your classes by packages correctly you will see that this is a better way. 如果您正确地按类别分隔您的类,您将看到这是一种更好的方法。

Users will tell you thanks. 用户会告诉你谢谢。

I had the same question some time ago, after several tries I selected this method and I am happy now. 前段时间我有同样的问题,经过几次尝试,我选择了这种方法,现在我很高兴。

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

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