如何解决在RestApi Spring中放置超过5个@Pathvariable的最佳方法是什么?
我在一个端点中有太多@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,并且在方法参数中而不是分别检索每个值,而是检索Map
此映射将包含请求中发送的所有路径变量,并且可以使用URI中使用的变量名称来访问值。
@GetMapping("/city/{cityname}/{priceLess}/{priceGreater}/{untilityLess}/{untilityGreater}/{rentORbuy}/{furnished}/{renovated}/{roomsLess}/{roomsGreater}")
public ResponseEntity<List<Flat>> findCity(@PathVariable Map<String,String> pathVariables){}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。