首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >api接口中参数的约束

api接口中参数的约束
EN

Stack Overflow用户
提问于 2015-11-28 21:40:09
回答 1查看 48关注 0票数 0

我在一个接口中声明了一个API调用,并且想知道是否可以对一些参数进行约束。我正在访问的API也有这些约束,并希望在我的程序中强制执行它们。

代码语言:javascript
复制
@GET("/recipes/search")
Call<RecipeResponse> getRecipes(
        @Query("cuisine") String cuisine,
        @Query("diet") String diet,
        @Query("excludeIngredients") String excludeIngredients,
        @Query("intolerances") String intolerances,
        @Query("number") Integer number,
        @Query("offset") Integer offset,
        @Query("query") String query,
        @Query("type") String type
);

我该怎么做呢?

我知道使用POST请求可以做到这一点,并通过@Body注释通过RequestBody传递对象。GET请求也可以这样做吗?在GET请求中,信息是通过查询字符串传递的。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-11-29 04:07:04

我想我最终找到了一个解决方案。我已经创建了一个类SearchRecipeRequest,在这个类中我将所有可能的参数声明为类变量。在setter中,我执行数据验证,比如检查所需参数上的null,或者端点指定的整数上的最小/最大值约束。然后,我创建了一个SearchRecipeRequestBuilder类来构建这样的对象,以便更容易地处理所有这些可能的参数:

代码语言:javascript
复制
public class SearchRecipeRequestBuilder {

    private String  _cuisine = null,
                    _diet = null,
                    _excludeIngredients = null,
                    _intolerances = null,
                    _query = null,
                    _type = null;

    private Integer _number = null,
                    _offset = null;

    public SearchRecipeRequestBuilder() {}

    public SearchRecipeRequest buildRequest() {
        return new SearchRecipeRequest(_cuisine, _diet, _excludeIngredients, _intolerances, _number, _offset, _query, _type);
    }

    public SearchRecipeRequestBuilder cuisine(String cuisine) {
        _cuisine = cuisine;
        return this;
    }

    public SearchRecipeRequestBuilder diet(String diet) {
        _diet = diet;
        return this;
    }

    public SearchRecipeRequestBuilder excludeIngredients(String excludeIngredients) {
        _excludeIngredients = excludeIngredients;
        return this;
    }

    public SearchRecipeRequestBuilder intolerances(String intolerances) {
        _intolerances = intolerances;
        return this;
    }

    public SearchRecipeRequestBuilder query(String query) {
        _query = query;
        return this;
    }

    public SearchRecipeRequestBuilder type(String type) {
        _type = type;
        return this;
    }

    public SearchRecipeRequestBuilder number(Integer number) {
        _number = number;
        return this;
    }

    public SearchRecipeRequestBuilder offset(Integer offset) {
        _offset = offset;
        return this;
    }
}

这让我可以像这样构建请求:

代码语言:javascript
复制
    SearchRecipeRequest request = new SearchRecipeRequestBuilder()
            .query("burger")
            .buildRequest();

然后,我将该对象传递给另一个函数,该函数知道如何使用请求对象将其传递给API。

这就是我现在正在做的,如果有人有更好的方式,我很乐意听到。:)

我从一个不同的StackOverflow问题Managing constructors with many parameters in Java中获得了使用构建器模式的想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33971986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档