繁体   English   中英

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

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

我有一个接收各种参数的 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"})

正如您所注意到的,问题在于有一些参数(年龄、位置)对于所有这些端点都是通用的。

计划是我们可能需要为所有这些端点引入一个新参数,如“性别”。 是否有最佳实践来跨 API 处理这些通用参数,以便我们不需要修改每个 Controller 并添加新添加的请求参数?

控制器看起来像这样:

@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);
    }

}

如果路径不一样,你不需要在@GetMapping注解中设置params字段,你可以简单地做:

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

如果它是相同的路径,您也可以这样做,但是您需要在 RequestParam 注释中添加“required=false”并手动处理当某些字段存在或不存在时要执行的操作

如果您不知道请求中有多少参数,您可以使用 @RequestParam 作为 Map,如下所示 -

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

您可以按如下方式传递这些参数 -

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

或者 -

@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);
}

要求 -

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

或者-

@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);
}

要求 -

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

我相信您的查询参数是一个单一的属性,它有多个值,年龄/位置是最后出现的其他一些强制性查询参数。 这就是我将如何编写已知给定参数的控制器

         @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;
        }

这个控制器的调用看起来像这样

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

现在,您可以根据字母表中的值创建连接点。

谢谢你。

暂无
暂无

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

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