Java 使用Spring,我可以创建多个可选路径变量吗?

Java 使用Spring,我可以创建多个可选路径变量吗?,java,spring,rest,Java,Spring,Rest,我尝试在Spring中使用几个可选的路径变量,比如/country/region/city/Chicago/street或/country/USA/region/Indiana/city/street/,我只想对这类变量使用一种方法 @GetMapping(value = "/country/{country}/region/{region}/city/{city}/street/{street}") public Street getStreet(@PathVariable

我尝试在Spring中使用几个可选的路径变量,比如
/country/region/city/Chicago/street
/country/USA/region/Indiana/city/street/
,我只想对这类变量使用一种方法

@GetMapping(value = "/country/{country}/region/{region}/city/{city}/street/{street}")
public Street getStreet(@PathVariable String country,
                        @PathVariable String region,
                        @PathVariable String city,
                        @PathVariable String street) {
    return new Street ();
}
但如果我想使用几个path变量,我必须使用@GetMapping注释的所有值组合,比如

@GetMapping(value = {
                        "/country/{country}/region/{region}/city/{city}/street/{street}",
                        "/country/region/{region}/city/{city}/street/{street}",
                        "/country/{country}/region/city/{city}/street/{street}",
                        /*
                          intermediate combinations
                        */
                        "/country/region/city/street/",
                    })
public Street getStreet(@PathVariable(required = false) String country,
                        @PathVariable(required = false) String region,
                        @PathVariable(required = false) String city,
                        @PathVariable(required = false) String street) {
    return new Street ();
}

如何减少组合的数量?也许,最好的方法是使用@RequestParam,但我必须使用@PathVariable

看起来您可以使用查询字符串,例如
GET/street?country=…®ion=…
。为什么必须使用路径变量?因为
/country/region/city/street/
/country/{country}是不同的路径/region/city/street/
,您必须单独指定。这是请求参数的用途。