繁体   English   中英

SpringBoot中字段验证失败时如何在ConstraintViolationException中获取RequestParam名称

[英]How to get the RequestParam name in ConstraintViolationException when field validation fails in SpringBoot

有没有办法在springboot验证中检索RequestParam名称(不是字段变量名称)?

例如

@GetMapping(value = "/abc")
public Response getABC(HttpServletRequest request, @RequestParam(name = user_number, defaultValue = "1") @Positive Integer number) {

// other code logic

}

这里当 user_number 验证失败时,有没有办法在 ConstraintViolationException 中捕获 RequestParam 名称(user_number)并显示在错误消息中? 我知道有可以从中提取字段名称的 PropertyPath,但我希望获取 RequestParam 名称。

您必须使用 support 和 validate 方法实现 org.springframework.validation.Validator 接口。

此处提供了有关此内容的详细教程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM