Java 如何Spring@requestparam验证字符串在字符串列表中
我试图用@Valid注释替换下面控制器中的硬编码验证Java 如何Spring@requestparam验证字符串在字符串列表中,java,spring,validation,spring-mvc,Java,Spring,Validation,Spring Mvc,我试图用@Valid注释替换下面控制器中的硬编码验证 @GetMapping(value = "/fruits") public List<String> fruits( @RequestParam(value = "fruitType", defaultValue = "") String fruitType) { final ImmutableList<String> fruitTypes = ImmutableList.of("C
@GetMapping(value = "/fruits")
public List<String> fruits(
@RequestParam(value = "fruitType", defaultValue = "") String fruitType) {
final ImmutableList<String> fruitTypes =
ImmutableList.of("Citrus", "Seed Less", "Tropical");
if (!fruitTypes.contains(fruitType)) {
throw new RuntimeException("Invalid Fruit type");
}
final ImmutableList<String> fruits =
ImmutableList.of("Apple", "Banana", "Orange");
//filter fruits based on type, then return
return fruits;
}
@GetMapping(value=“/fruits”)
公开上市水果(
@RequestParam(value=“fruitType”,defaultValue=”“)字符串(fruitType){
最终不可变列表水果类型=
“柑橘”、“无籽”、“热带”的不可变列表;
如果(!水果类型.包含(水果类型)){
抛出新的RuntimeException(“无效的结果类型”);
}
最终不可变列表结果=
“苹果”、“香蕉”、“橙子”的不可变列表;
//根据类型过滤水果,然后返回
还果;
}
我知道我可以用@Pattern用正则表达式检查
@GetMapping(value = "/fruits")
public List<String> fruits(@RequestParam(value = "fruitType", defaultValue = "")
@Valid @javax.validation.constraints.Pattern(regexp="Citrus|Seed Less|Tropical")
String fruitType) {
// final ImmutableList<String> fruitTypes = ImmutableList.of("Citrus", "Seed Less", "Tropical");
// if (!fruitTypes.contains(fruitType)) {
// throw new RuntimeException("Invalid Fruit type");
// }
final ImmutableList<String> fruits = ImmutableList.of("Apple", "Banana", "Orange");
//filter fruits based on type, then return
return fruits;
}
@GetMapping(value=“/fruits”)
公开列表水果(@RequestParam(value=“fruitType”,defaultValue=”“)
@Valid@javax.validation.constraints.Pattern(regexp=“Citrus | Seed Less | Tropical”)
字符串类型){
//最终不可免疫列表水果类型=不可免疫列表(“柑橘”、“无籽”、“热带”);
//如果(!水果类型.包含(水果类型)){
//抛出新的RuntimeException(“无效的结果类型”);
// }
最终ImmutableList水果=ImmutableList.of(“苹果”、“香蕉”、“橙子”);
//根据类型过滤水果,然后返回
还果;
}
但是如果果型列表不是静态的
还有其他spring方法吗?当您根据自定义列表进行验证时,没有现成的方法可以做到这一点。但是,要验证类型,您可以将
水果类型
定义为枚举
,并使用@RequestParam
对其进行注释,例如:
enum FruitType{
Apple,
Banana,
Orange;
}
public List<String> fruits(
@RequestParam(value = "fruitType") FruitTypefruitType) {
//Implementation
枚举类型{
苹果,
香蕉,
橙色
}
公开上市水果(
@RequestParam(value=“水果类型”)水果类型水果类型{
//实施
您可以使用enum验证值并抛出自定义错误,您可以使用@RestControllerAdvice。下面是示例代码
enum FruitType {
Apple, Banana, Orange;
}
@GetMapping(value = "/fruits")
public List<String> fruits(@RequestParam(value = "fruitType") FruitType fruitType) {
// You can put your business logic here.
return fruits;
}
枚举类型{
苹果、香蕉、橘子;
}
@GetMapping(value=“/fruits”)
公共列表水果(@RequestParam(value=“fruitType”)水果类型水果类型){
//您可以将业务逻辑放在这里。
还果;
}
//下面是控制器顾问的示例类
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<MpsErrorResponse> exceptionToDoHandler(HttpServletResponse response,
MethodArgumentTypeMismatchException ex) throws IOException {
return new ResponseEntity<>(new MpsErrorResponse(HttpStatus.NOT_FOUND.value(), "Invalid Fruit type"),
HttpStatus.NOT_FOUND);
}
}
@RestControllerAdvice
公共类GlobalExceptionHandler{
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
公共响应例外DoHandler(HttpServletResponse,
MethodArgumentTypeMismatchException ex)引发IOException{
返回新的ResponseEntity(新的MpsErrorResponse(HttpStatus.NOT_FOUND.value(),“无效的水果类型”),
HttpStatus。未找到);
}
}
编写您自己的约束注释和关联的验证器,以检查动态值列表中的值。水果类型的列表不是静态的”我想了想,但正如您看到的,水果类型可以有空格,这就是为什么我包含“无种子”的原因作为类型之一。这也不是静态列表,我已经将其作为一个静态列表包含在代码中以消除混乱