如何验证@RequestQuery的值

时间:2018-07-02 14:08:07

标签: spring rest spring-mvc

我正在尝试在REST URI中的等号之后验证以下查询参数的值,请问有人知道如何使用Spring 4.1吗?

我想在方法参数中验证'drive'是传递的参数,但我能验证的只是操作部分

我想做类似@RequestParam(value = "drive")

的事情

localhost / test?operation = drive

@RequestMapping(value = "/test", method = RequestMethod.PUT)
public ResponseEntity<Void> operation(@RequestParam(value = "operation", required = true) String operation)

`

1 个答案:

答案 0 :(得分:1)

您可以使用Bean Validation annotations

如果要检查是否允许该值,可以将@Pattern与正则表达式配合使用:

@Pattern(regexp = "value1|value2|value3", flags = Pattern.Flag.CASE_INSENSITIVE)
@RequestMapping(value = "/test", method = RequestMethod.PUT)
public ResponseEntity<Void> operation(
    @RequestParam(value = "operation", required = true)
    @Pattern(regexp = "value1|value2|value3", flags = Pattern.Flag.CASE_INSENSITIVE)
    String operation) {
    ... 
}