我正在开发spring-boot应用程序,我需要在检查其他信息之前验证用户信息。这是一个片段:向@Valid发送请求
@RequestMapping(value = "/create-user", method = RequestMethod.POST)
public String ceateCustomer(@Valid @RequestBody User user){
myService.create(user);
}
当用户来自另一个途径/创建-SP-用户,我不得不重新有效用户:
@RequestMapping(value = "/create-sp-user", method = RequestMethod.POST)
public String ceateCustomer(@Valid @RequestBody User user){
user.setSp(true);
// I tried @Valid user without success
// In my User class if sp = true the field telNumber is mandatory
myService.create(user);
}
请问您有什么想法?
问候
您可以轻松编写自己的自定义验证程序,因为没有默认的验证器可以根据其他值检查必填字段。 –
对不起,我没有得到你想要的。来自客户端的用户结构是什么?或者是,你想在**设置sp = true之后验证用户**?在这种情况下,您可能需要按照此处所述编写自定义验证程序:https://stackoverflow.com/questions/12146298/spring-mvc-how-to-perform-validation - @valid注释会在*之前检查它* *任何代码在控制器中执行。 – Dominik
谢谢@Dominik是的,我必须验证用户设置为真正后 – Olivier