接着上篇博客学习。上篇博客是已经基本完成用户模块的注册接口的开发。springboot实战学习笔记(3)(Lombok插件、postman测试工具、MD5加密算法、post请求、接口文档、注解、如何在IDEA中设置层级显示包结构、显示接口中的方法)-CSDN博客本篇博客主要是关于用户模块的”注册“的后端所有接口的写法。其中主要包括:插件lombok(自动生成getter、setter等等方法)、注解@Data、接口开发文档、post请求、Md5加密算法运用、如何在IDEA中设置包结构层级展示以及设置所以接口中的方法展示、测试工具postman的使用等等......
https://blog.csdn.net/m0_74363339/article/details/142312514但是忘记去做一个参数校验(因为接口文档中对username、password有一个限制)。我们要保证前端传进来的参数,不符合这个规则,是不能完成注册的!!
目录
1、对参数"username"与"password"进行手动校验。
2、回看手动参数校验。
3、参数校验框架。Spring Validation。
(1)基本介绍
(2)基本使用场景
(3)总体作用
(4)在该注册接口中的具体操作
第一步。在pom文件中添加Validation依赖。
第二步。在需要校验的参数前添加注解@Pattern,并且给它里面的参数"regexp"赋值(正则表达式)。其中"\\S"的介绍如下。
第三步。需要在本类上使用注解@Validated。
重启工程,并在测试工具postman中测试接口。
因为上面测试时,参数调用失败,则会抛出一个异常如上。然后显示一个status:"500",这不知道哪里出了问题。不符合接口文档里面提供的响应信息的格式。所以得继续完善,对上面的异常进行处理。
4、全局异常处理器(处理参数校验失败的异常)(GlobalExceptionHandler)
(1)基本介绍
(2)在注册接口所在的项目"big-event"开始操作。
5、总结
1、对参数"username"与"password"进行手动校验。
package com.feisi.controller; import com.feisi.pojo.Result; import com.feisi.pojo.User; import com.feisi.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Title: UserController * @Author HeYouLong * @Package com.feisi.controller * @Date 2024/9/17 下午1:51 * @description: */ @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @PostMapping("/register") public Result register(String username, String password) { if(username!=null && username.length()>=5 && username.length()<=16 && password!=null && password.length()>=5 && password.length()<=16){ //查询用户 User user = userService.findByName(username); if(user==null){ //没有占用 //注册 userService.register(username,password); return Result.success(); }else { //被占用 return Result.error("用户名已被占用"); } }else { return Result.error("参数不合法"); } } }
2、回看手动参数校验。
- 往前看之前写的手动参数校验的代码非常繁琐,关是两个参数就很麻烦了,所以还需要改进才行!!
3、参数校验框架。Spring Validation。
(Spring 提供的一个参数校验框架,使用预定义的注解完成参数校验)
(1)基本介绍
- 提供了一种声明式验证机制,可以在不侵入业务代码的情况下,对数据进行校验。
(2)基本使用场景
- 在表单提交时验证用户输入的数据。
- 在数据持久化之前验证数据的完整性和正确性。
- 在服务层或业务层进行数据转换或处理前的验证。
(3)总体作用
- Spring Validation 提供了灵活且强大的数据验证能力,是开发过程中不可或缺的一个工具。
(4)在该注册接口中的具体操作
第一步。在pom文件中添加Validation依赖。
(第一次添加该依赖需要手动打,然后刷新Maven,本地仓库没有就让它去镜像里下载)
第二步。在需要校验的参数前添加注解@Pattern,并且给它里面的参数"regexp"赋值(正则表达式)。其中"\\S"的介绍如下。
(正则表达式中的"^$"是一个非常特殊的模式,它用来匹配空字符串,也就是说,这个模式会匹配那些长度为0的字符串,即没有任何字符的字符串。解释:"^"表示字符串的开始位置。"$"表示字符串的结束位置。当这两个符号连在一起使用时,它们之间没有任何其他字符,因此这个正则表达式只能匹配那些开始和结束位置重合的字符串,即空字符串)
("\S"是 另一个特殊的元字符,用于匹配任何非空白字符。不过,注意在大多数编程语言中,"反斜杠
\
"是一个转义字符,所以当你想要在字符串中表示一个反斜杠时,你通常需要写"两个反斜杠\\")@Pattern(regexp = "^$")
public Result register(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$") String password) { // // }
第三步。需要在本类上使用注解@Validated。
package com.feisi.controller; import com.feisi.pojo.Result; import com.feisi.pojo.User; import com.feisi.service.UserService; import jakarta.validation.constraints.Pattern; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Title: UserController * @Author HeYouLong * @Package com.feisi.controller * @Date 2024/9/17 下午1:51 * @description: */ @RestController @RequestMapping("/user") @Validated public class UserController { @Autowired private UserService userService; @PostMapping("/register") public Result register(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$") String password) { //查询用户 User user = userService.findByName(username); if(user==null){ //没有占用 //注册 userService.register(username,password); return Result.success(); }else { //被占用 return Result.error("用户名已被占用"); } } }
重启工程,并在测试工具postman中测试接口。
因为上面测试时,参数调用失败,则会抛出一个异常如上。然后显示一个status:"500",这不知道哪里出了问题。不符合接口文档里面提供的响应信息的格式。所以得继续完善,对上面的异常进行处理。
4、全局异常处理器(处理参数校验失败的异常)(GlobalExceptionHandler)
(1)基本介绍
- 创建一个类。然后在类上添加注解@RestControllerAdvice。用它来标识这个类是用来处理异常的。
- 因为我们添加的是注解@RestXXX。所以这个类里面的所有方法的返回值都会被转换成JSON字符串,响应给浏览器。
- 然后在类里面需要添加一个方法去处理异常。在方法上需要添加注解@ExceptionHandler(Exception.class)。"Exception.class"是指处理所有的异常。
- 这个方法的返回值是result。当出了异常,返回的结果也是会满足接口文档的要求。
(2)在注册接口所在的项目"big-event"开始操作。
- 在com.feisi包下创建一个包(exception)。
- 再在包下创建一个类"GlobalExceptionHandler"。
- 在类上添加一个注解@RestControllerAdvice。在类的内部添加一个方法handException(),方法上要写一个注解@ExceptionHandler(Exception.class)。方法返回值是Result对象。在Exception会封装一个错误提示信息"e.getMessge()",但是一些异常信息并没有封装。所以还要用到Spring提供的String字符串的工具类"StringUtils"的hasLength()方法,问返回的return的"e.getMessage()"是否存在错误原因,若没有就返回"操作失败"。
package com.feisi.exception; import com.feisi.pojo.Result; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; /** * @Title: GlobalExceptionHandler * @Author HeYouLong * @Package com.feisi.exception * @Date 2024/9/18 下午2:59 * @description: */ @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public Result handleException(Exception e) { //在方法里参数要声明一个变量去接收,一旦捕获到异常对象就要处理 e.printStackTrace(); return Result.error(StringUtils.hasLength(e.getMessage())?e.getMessage():"操作失败"); } }
- 重启工程。再次去postman测试接口。
5、总结
- 本篇博客使用Validation对注解接口的参数进行了校验。
- 要使用Validation需要做的几步如下。(其中它不只有一个注解@Pattern...)