引入依赖
<!--参数效验--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><!--@Length参数效验--><dependency><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId></dependency>
前端参数请求类SysMenu
package com.sky.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;/*** 菜单权限表 sys_menu*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SysMenu extends BaseEntity{private static final long serialVersionUID = 1L;/** 菜单ID */private Long menuId;/** 菜单名称 */@Length(min=4,max=12,message = "菜单名称在4到12之间")@NotBlank(message = "menuName 不能为空")private String menuName;/** 父菜单ID */private Long parentId;/** 显示顺序 */private String orderNum;/** 菜单URL */@Length(min=4,max=12,message = "url在4到12之间")@NotBlank(message = "url 不能为空")private String url;/** 打开方式(menuItem页签 menuBlank新窗口) */private String target;/** 类型(M目录 C菜单 F按钮) */private String menuType;/** 菜单状态(0显示 1隐藏) */private String visible;/** 是否刷新(0刷新 1不刷新) */private String isRefresh;/** 权限字符串 */private String perms;/** 菜单图标 */private String icon;/** 子菜单 */private List<SysMenu> children = new ArrayList<SysMenu>();}
参数校验常用注解
除了前四个 @Null,@ NotNull,@ NotBlank,@NotEmpty外,其他所有的注解,传 null 时都会被当作有效处理
注解常用参数值:message(校验不通过反馈信息)
JSR303定义的基础校验类型和Hibernate Validator 中附加的 constraint :@Length,@Range
全局异常处理@RestControllerAdvice
package com.sky.handler;import com.sky.constant.MessageConstant;
import com.sky.exception.BaseException;
import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;import javax.validation.ConstraintViolationException;
import java.sql.SQLIntegrityConstraintViolationException;/*** 全局异常处理器,处理项目中抛出的业务异常*/
@RestControllerAdvice //是一个用于全局异常处理的注解
@Slf4j
public class GlobalExceptionHandler {/*** 捕获业务异常* @param ex @ExceptionHandler注解用于标识一个方法,该方法用于处理由Spring MVC控制器中抛出的异常。当控制器中的代码抛出异常时,Spring MVC会查找具有@ExceptionHandler注解的方法,并执行该方法来处理异常。* @return*/@ExceptionHandlerpublic Result exceptionHandler(BaseException ex){log.error("异常信息:{}", ex.getMessage());return Result.error(ex.getMessage());}/** 处理sql异常 捕获了之后 控制台就不会输出异常信息 直接抛出到前端了* */@ExceptionHandlerpublic Result exceptionHandler(SQLIntegrityConstraintViolationException ex){//Duplicate entry 'zhansan' for key 'employee.idx_username'String message=ex.getMessage();if(message.contains("Duplicate entry")){//根据空格分隔String[] split=message.split(" ");//取到用户名String username=split[2];String msg=username+"已存在";return Result.error(msg);}else {//引入常量类字符串 方便后期维护 也可以直接写字符串表示return Result.error(MessageConstant.UNKNOWN_ERROR);}}/** 添加效验参数异常处理* */@ExceptionHandler(BindException.class)public Result bindExceptionHandler(BindException e){log.error("出现了异常!{}",e.getMessage());return Result.error(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());}/** 添加效验参数异常处理get请求参数 技巧:控制台报什么错误 就截取错误的类型来进行捕获* */@ExceptionHandler(ConstraintViolationException.class)public Result violatExceptionHandler(ConstraintViolationException e){log.error("出现了异常!{}",e.getMessage());/*截取冒号后面的字符串*/return Result.error(e.getMessage().split(":")[1]);}}
Contrller使用 @Validated @Valid
package com.sky.controller.admin;import com.sky.annotation.RequiresPermissions;
import com.sky.dto.SysMenuQueryDTO;
import com.sky.entity.SysMenu;
import com.sky.result.Result;
import com.sky.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;@Validated //对本类的方法开启验证功能
@RestController
@RequestMapping("/admin/system/menu")
@Api(tags = "菜单栏权限接口")
@Slf4j
public class SysMenuContrller {@Autowiredprivate SysMenuService sysMenuService;/*** 查询系统菜单列表*/@GetMapping("/list")@RequiresPermissions("postCreateRule,POST")@ApiOperation("查询菜单列表")public Result<List<SysMenu>> list(SysMenuQueryDTO sysMenuQueryDTO){List<SysMenu> sysMenuList=sysMenuService.selectMenuList(sysMenuQueryDTO);return Result.success(sysMenuList);}/*** 查询系统菜单树列表*/@GetMapping("/treeselect")@ApiOperation("查询菜单树列表")public Result<List<SysMenu>> treeselect(){List<SysMenu> sysMenuList=sysMenuService.menuTreeData();return Result.success(sysMenuList);}/** 新增保存菜单* */@PostMapping("/add")@ApiOperation("新增菜单")public Result addSave(@RequestBody @Valid/*效验后面的参数*/ SysMenu sysMenu){log.info("新增菜单:{}",sysMenu);//验证菜单名称是否重复sysMenuService.insertMenu(sysMenu);return Result.success();}/** 测试参数验证 @NotBlank这个需要和 @Validated 一起使用* */@GetMapping("/addlist")@ApiOperation("参数验证")public Result canshu(@NotBlank(message = "name 不能为空") String name){log.info("参数验证:{}",name);return Result.success();}
}
后端统一返回类result
package com.sky.result;import lombok.Data;
import java.io.Serializable;/*** 后端统一返回结果* @param <T>*/
@Data
public class Result<T> implements Serializable {private Integer code; //编码:1成功,0和其它数字为失败private String msg; //错误信息private T data; //数据public static <T> Result<T> success() {Result<T> result = new Result<T>();result.code = 1;return result;}public static <T> Result<T> success(T object) {Result<T> result = new Result<T>();result.data = object;result.code = 1;return result;}public static <T> Result<T> error(String msg) {Result result = new Result();result.msg = msg;result.code = 0;return result;}}