简体   繁体   English

具有相同参数的多个 REST API 调用的最佳实践是什么

[英]What is the best practice for multiple REST API calls with the same parameters

I have a list of REST APIs which receive various parameters, which are aimed to search, filter and return data back to the front-end.我有一个接收各种参数的 REST API 列表,这些参数旨在搜索、过滤数据并将数据返回给前端。

@GetMapping(value = "/api1", params = {"x,y,z,age,location"})
@GetMapping(value = "/api2", params = {"a,b,c,d,age,location"})
@GetMapping(value = "/api3", params = {"p,q,r,s,,age,location"})
@GetMapping(value = "/api4", params = {"p,q,r,s,,age,location"})
@GetMapping(value = "/api5", params = {"p,q,r,s,,age,location"})

As you can notice, the problem is that there are a few parameters (age,location), which are common for all these endpoints.正如您所注意到的,问题在于有一些参数(年龄、位置)对于所有这些端点都是通用的。

The plan is we might need to introduce a new parameter like 'gender' to all these endpoints.计划是我们可能需要为所有这些端点引入一个新参数,如“性别”。 Is there a best practice to handle these common parameters across the APIs so that we don't need to modify each Controller and add the newly added request parameter?是否有最佳实践来跨 API 处理这些通用参数,以便我们不需要修改每个 Controller 并添加新添加的请求参数?

The controller would look something like this:控制器看起来像这样:

@RestController
public class UserFilterController {


    @GetMapping(path = "/api1")
    public ResponseEntity filterUserWithApi1(String x, String y, String z, String age, String location) {

        return new ResponseEntity(HttpStatus.OK);
    }

    @GetMapping(path = "/api2")
    public ResponseEntity filterUserWithApi2(String a, String b, String c, String age, String location) {

        return new ResponseEntity(HttpStatus.OK);
    }

    @GetMapping(path = "/api3")
    public ResponseEntity filterUserWithApi3(String d, String e, String f, String age, String location) {

        return new ResponseEntity(HttpStatus.OK);
    }

    @GetMapping(path = "/api4")
    public ResponseEntity filterUserWithApi4(String g, String s, String h, String age, String location) {

        return new ResponseEntity(HttpStatus.OK);
    }

    @GetMapping(path = "/api5")
    public ResponseEntity filterUserWithApi5(String j, String k, String l, String age, String location) {

        return new ResponseEntity(HttpStatus.OK);
    }

}

If the path it's not the same, you don't need to set the params field in the @GetMapping annotation, you can simply do :如果路径不一样,你不需要在@GetMapping注解中设置params字段,你可以简单地做:

@GetMapping(path = "/api1")
public void myFunction( @RequestParam("age") String age, @RequestParam("location") String location, ... ) {
...
}

If it's the same path, you can also do that, but you will need to add "required=false" in the RequestParam annotation and handle manually what to do when some fields are present or not如果它是相同的路径,您也可以这样做,但是您需要在 RequestParam 注释中添加“required=false”并手动处理当某些字段存在或不存在时要执行的操作

If you don't know how many params will be there in request you can use @RequestParam as Map like below -如果您不知道请求中有多少参数,您可以使用 @RequestParam 作为 Map,如下所示 -

@GetMapping(path = "/api1")
public void test(@RequestParam Map<String, String> parameters) {
    //TODO
    String value1 = parameters.get("key1");
    ........
}

And you can pass these params as below -您可以按如下方式传递这些参数 -

/api1?key1=value1&key2=value2...

OR -或者 -

@GetMapping(path = "/api1/{age}/{location}")
public ResponseEntity filterUserWithApi1(@PathVariable("age") String age,
                                         @PathVariable("location") String location,
                                         @RequestParam("x") String x,
                                         @RequestParam("y") String y,
                                         @RequestParam("z") String z) {
    return new ResponseEntity(HttpStatus.OK);
}

Request -要求 -

/api1/24/earth?x=x_value&y=y_value&z=z_value

OR-或者-

@GetMapping(path = "/api1/{age}/{location}/{x}/{y}/{z}")
public ResponseEntity filterUserWithApi1(@PathVariable("age") String age,
                                         @PathVariable("location") String location,
                                         @PathVariable("x") String x,
                                         @PathVariable("y") String y,
                                         @PathVariable("z") String z) 
{
    return new ResponseEntity(HttpStatus.OK);
}

REQUEST -要求 -

/api1/<age_value>/<location_value>/<x_value>/<y_value>/<z_value>

I believe your query parameter is a single attribute which has number of values and age/location are some other mandatory query param which is present at the end.我相信您的查询参数是一个单一的属性,它有多个值,年龄/位置是最后出现的其他一些强制性查询参数。 This is how I will write the controller known the given parameters这就是我将如何编写已知给定参数的控制器

         @GetMapping(path = "/test")
        public Map<String, String> test(@RequestParam("alphabets") Set<String> alphabets,
                                        @RequestParam("age") int age,
                                        @RequestParam("location") String location) {
            final Map<String, String> responseMap = new HashMap<>();
            responseMap.put("alphabets", alphabets.toString());
            responseMap.put("age", Integer.toString(age));
            responseMap.put("location", location);
            return responseMap;
        }

The invocation of this controller looks like this这个控制器的调用看起来像这样

http://localhost:8080/test?alphabets=a&age=1&location=test
http://localhost:8080/test?alphabets=a,b,c&age=1&location=test

Now you can create the junction based on the values present in the alphabets.现在,您可以根据字母表中的值创建连接点。

Thank you.谢谢你。

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

相关问题 REST API客户端实现,最佳实践是什么? - REST API client implementation, what is the best practice? 内部Java代码最佳实践,用于处理无效的REST API参数 - Internal Java code best practice for dealing with invalid REST API parameters 关于应执行多少次API调用的最佳实践是什么? - What is the best practice on how many calls to the API shall be done? 在rest API响应中返回登录凭据的最佳实践是什么? - What is the best practice to return login credentials in a rest API response? Rest API最佳实践:多参数搜索过滤器API实现 - Rest API Best Practices: Multiple parameters search filter API implementation REST API的多个参数 - Multiple parameters for REST API 同步并行API请求以从长时间运行的查询中返回相同结果而不进行多次执行的最佳实践是什么? - What is the best practice for synchronizing parallel API requests to return the same result from a long running query without multiple executions? 为REST API构建Java SDK的最佳实践 - Best Practice to Build a Java SDK for a REST API 在JSP中验证参数的最佳实践是什么? - What is the best practice for validating parameters in JSP? 在方法内“回滚”REST方法调用的最佳实践 - Best practice to 'rollback' REST method calls inside method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM