Java 在Spring中放置5个以上@Pathvariable的最佳方法是什么?

Java 在Spring中放置5个以上@Pathvariable的最佳方法是什么?,java,spring,restapi,Java,Spring,Restapi,我在一个端点中有太多@PathVariable。我如何最小化它,因为它看起来没完没了,也许可以用我需要的所有变量再创建一个对象,并使用@RequestBody或任何其他方式 @GetMapping("/city/{cityname}/{priceLess}/{priceGreater}/{untilityLess}/{untilityGreater}/{rentORbuy}/{furnished}/{renovated}/{roomsLess}/{roomsGreater}"

我在一个端点中有太多@PathVariable。我如何最小化它,因为它看起来没完没了,也许可以用我需要的所有变量再创建一个对象,并使用@RequestBody或任何其他方式

 @GetMapping("/city/{cityname}/{priceLess}/{priceGreater}/{untilityLess}/{untilityGreater}/{rentORbuy}/{furnished}/{renovated}/{roomsLess}/{roomsGreater}")
    public ResponseEntity<List<Flat>> findCity(@PathVariable("cityname") String cityname,
                                               @PathVariable("priceLess") BigDecimal priceLess,
                                               @PathVariable("priceGreater") BigDecimal priceGreater,
                                               @PathVariable("untilityLess") BigDecimal untilityLess,
                                               @PathVariable("untilityGreater") BigDecimal untilityGreater,
                                               @PathVariable("rentORbuy") Boolean rentORbuy,
                                               @PathVariable("furnished") Boolean furnished,
                                               @PathVariable("renovated") Boolean renovated,
                                               @PathVariable("roomsLess") int roomsLess,
                                               @PathVariable("roomsGreater") int roomsGreater){}
@GetMapping(“/city/{cityname}/{priceLess}/{pricemorer}/{untilityLess}/{untilityGreater}/{rentORbuy}/{profected}/{rentured}/{roomsLess}/{roomsGreater}”)
public ResponseEntity findCity(@PathVariable(“cityname”)字符串cityname,
@PathVariable(“无价”)BigDecimal无价,
@PathVariable(“priceGreater”)BigDecimal priceGreater,
@PathVariable(“untilityLess”)BigDecimal untilityLess,
@PathVariable(“UntilityCreater”)BigDecimal UntilityCreater,
@路径变量(“rentORbuy”)布尔rentORbuy,
@路径变量(“已提供”)布尔值已提供,
@PathVariable(“翻新”)布尔值翻新,
@PathVariable(“roomsLess”)int-roomsLess,
@路径变量(“roomsGreater”)int roomsGreater{}

使用相同的@GetMapping,在方法参数中,而不是分别检索每个值来检索映射

此映射将包含请求中发送的所有路径变量,并且可以使用URI中使用的变量名访问这些值

@GetMapping("/city/{cityname}/{priceLess}/{priceGreater}/{untilityLess}/{untilityGreater}/{rentORbuy}/{furnished}/{renovated}/{roomsLess}/{roomsGreater}")
public ResponseEntity<List<Flat>> findCity(@PathVariable Map<String, String> pathVariables){}
@GetMapping(“/city/{cityname}/{priceLess}/{pricemorer}/{untilityLess}/{untilityGreater}/{rentORbuy}/{profected}/{rentured}/{roomsLess}/{roomsGreater}”)
公共响应查找城市(@PathVariable映射pathVariables){}

这看起来像是一个不可用的API。如果它是REST,那么过滤器不应该是路径的一部分。将它们放在查询字符串或帖子正文中(当尝试读取它们时,请注意它们中的大多数可能是可选的,因此不必存在)。前面的部分是什么?我假设路径
/city/{cityname}
是用来标识城市的,其他一切看起来都像可选的过滤器,因此应该是查询参数。因此,路径应该是
/city/{cityname}
,或者如果
cityname
也是可选的,甚至可能只是类似于
/search
的东西。如果你是新手,你应该先深入研究URL和REST,然后在春季研究如何映射它们。我已经提到了要查找的关键字:querypath。正在调用的url可能与查询路径中的筛选器类似:
../city/London?priceLess=1000&roomsGreater=3&rentOrBuy=rent
您可以使用@RequestParam获取查询或post body参数,但这可能会使方法签名非常臃肿。或者,您可以尝试获取请求本身的句柄(我不知道在Spring中如何做),并从中提取参数,或者使用一个json对象,您可以从中传递并提取参数(例如,
{“priceLess”:1000,“RoomsCreater”:3,“rentOrBuy”:“rent”}
可能会被解析为名为
过滤器的类。@Thomas谢谢,我会详细检查并尝试。