1 实体参数校验
2.1 操作
步骤一:在实体属性上一行添加注解校验
步骤二:为校验的实体变量添加@Validated注解
2.2 小结
- 实体类的成员变量上添加注解
• @NotNull• @NotEmpty2.接口方法的实体参数上添加@Validated注解
2 参数校验-@URL
3 参数校验-@Pattern
4 手写参数校验--重置密码
@PatchMapping("/updatePwd") public Result updatePwd(@RequestBody Map<String, String> params,@RequestHeader("Authorization") String token) { //1.校验参数 String oldPwd = params.get("old_pwd"); String newPwd = params.get("new_pwd"); String rePwd = params.get("re_pwd"); if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) { return Result.error("缺少必要的参数"); } //原密码是否正确 //调用userService根据用户名拿到原密码,再和old_pwd比对 Map<String,Object> map = ThreadLocalUtil.get(); String username = (String) map.get("username"); User loginUser = userService.findByUserName(username); if (!loginUser.getPassword().equals(Md5Util.getMD5String(oldPwd))){ return Result.error("原密码填写不正确"); } //newPwd和rePwd是否一样 if (!rePwd.equals(newPwd)){ return Result.error("两次填写的新密码不一样"); } //2.调用service完成密码更新 userService.updatePwd(newPwd); //删除redis中对应的token ValueOperations<String, String> operations = stringRedisTemplate.opsForValue(); operations.getOperations().delete(token); return Result.success(); }