如何检测整数是否传递给SpringBoot中的@PathVaraible批注

时间:2019-06-02 22:53:34

标签: spring-mvc

我正在尝试验证是否已将整数值传递给@PathVariable批注。我已经设置了以下配置文件:

@Configuration
public class SpringConfiguration {

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}

然后我的控制器中有以下内容

@RestController
@Validated
public class UserController {
@RequestMapping(value = "/users/{userID}", method = RequestMethod.GET)

public ResponseEntity<User> get(
  @PathVariable @NumberFormat(style = Style.NUMBER) int id)  {
  ///Code here
}

我可以使用什么注释来检测是否已传递整数?我已经测试了@Min和@Range批注,它们可以正常工作。

谢谢。

1 个答案:

答案 0 :(得分:0)

设法找到有关如何验证路径变量的解决方案 here 使用以下内容:

@Pattern(regexp = "\\d{9}", message = "Bank shoud be identified by exactly 9 digits")