@valid和@BindingResult 数据校验
记一个简单又好用的API:BindingResult
BindingResult使用起来很简单,一般在controller(控制器)中使用
作用:对前端传进来的参数进行校验,大大节省了必要的逻辑判断
相关代码实例
控制层:
@ApiOperation(value = "登录" , notes = "登录功能")
@PostMapping("api/delogin")
public Dto login(@Validated LoginParams loginParams, BindingResult br){
//jsr303验证
if (br.hasErrors()){
return DtoUtil.getFaild("30002","参数出错");
}
try {
LoginResponseVo responseVo = userService.login(loginParams);
return DtoUtil.getSuccess(responseVo);
}catch (LoginException e){
return DtoUtil.getSuccess("30002",e.getMessage());
}catch (Exception e