Request Param 的可选参数是一种不好的做法吗?
Optional parameters for Request Param is a bad practise?
我正在为我的 Rest 控制器使用可选参数,在我的例子中是为了区分调用哪个方法:
@GetMapping("/cars")
@ResponseBody
public List<CarsDTO> getAllCarsByCat(@RequestParam Optional<Integer> cat1,
@RequestParam Optional<Integer> cat2) {
if (cat1.isPresent() && cat2.isPresent())
return carsService.getAllCarsByCat1AndCat2(cat1.get(), cat2.get());
else if (cat1.isPresent())
return carsService.getAllCarsByCat1(cat1.get());
else if (cat2.isPresent())
return carsService.getAllCarsByCat2(cat2.get());
else
return carsService.getAllCars();
}
为什么下面帖子的最高投票响应建议 "Using Optional parameters causing conditional logic inside the methods is literally contra-productive."?
我正是这样做的,并将其视为最易读和最直接的解决方案。这种方法有什么问题?
将 Optional
用作 @RequestParam
的唯一问题是性能和使用 Optional.OfNullable
创建可选包装器并使用 Optional.get()
展开或使用 Optional.ifPresent()
检查。从功能的角度来看,使用 Optional
总是看起来不错,但作为一个优秀的程序员,它是一个不必要的额外操作包装和解包。在 spring 中 Optional
允许在 @RequestParam 中将参数声明为可选
By default, method parameters that use this annotation are required, but you can specify that a method parameter is optional by setting the @RequestParam annotation’s required flag to false or by declaring the argument with an java.util.Optional wrapper.
所以你可以简单地使用 required==false
将那些 @RequestParam
设为可选,然后使用 if else 遵循相同的方法,你也可以使用 Objects.nonNull for more readability or you can also use defaultValue
@GetMapping("/cars")
@ResponseBody
public List<CarsDTO> getAllCarsByCat(@RequestParam(name="cat1", required=false) Integer cat1,
@RequestParam(name="cat2", required=false) Integer cat2) {
if (cat1!=null && cat2!=null)
return carsService.getAllCarsByCat1AndCat2(cat1, cat2);
else if (cat1!=null)
return carsService.getAllCarsByCat1(cat1);
else if (cat2!=null)
return carsService.getAllCarsByCat2(cat2);
else
return carsService.getAllCars();
}
我正在为我的 Rest 控制器使用可选参数,在我的例子中是为了区分调用哪个方法:
@GetMapping("/cars")
@ResponseBody
public List<CarsDTO> getAllCarsByCat(@RequestParam Optional<Integer> cat1,
@RequestParam Optional<Integer> cat2) {
if (cat1.isPresent() && cat2.isPresent())
return carsService.getAllCarsByCat1AndCat2(cat1.get(), cat2.get());
else if (cat1.isPresent())
return carsService.getAllCarsByCat1(cat1.get());
else if (cat2.isPresent())
return carsService.getAllCarsByCat2(cat2.get());
else
return carsService.getAllCars();
}
为什么下面帖子的最高投票响应建议 "Using Optional parameters causing conditional logic inside the methods is literally contra-productive."?
我正是这样做的,并将其视为最易读和最直接的解决方案。这种方法有什么问题?
将 Optional
用作 @RequestParam
的唯一问题是性能和使用 Optional.OfNullable
创建可选包装器并使用 Optional.get()
展开或使用 Optional.ifPresent()
检查。从功能的角度来看,使用 Optional
总是看起来不错,但作为一个优秀的程序员,它是一个不必要的额外操作包装和解包。在 spring 中 Optional
允许在 @RequestParam 中将参数声明为可选
By default, method parameters that use this annotation are required, but you can specify that a method parameter is optional by setting the @RequestParam annotation’s required flag to false or by declaring the argument with an java.util.Optional wrapper.
所以你可以简单地使用 required==false
将那些 @RequestParam
设为可选,然后使用 if else 遵循相同的方法,你也可以使用 Objects.nonNull for more readability or you can also use defaultValue
@GetMapping("/cars")
@ResponseBody
public List<CarsDTO> getAllCarsByCat(@RequestParam(name="cat1", required=false) Integer cat1,
@RequestParam(name="cat2", required=false) Integer cat2) {
if (cat1!=null && cat2!=null)
return carsService.getAllCarsByCat1AndCat2(cat1, cat2);
else if (cat1!=null)
return carsService.getAllCarsByCat1(cat1);
else if (cat2!=null)
return carsService.getAllCarsByCat2(cat2);
else
return carsService.getAllCars();
}