项目笔记(一) --- 通用内容

76 阅读2分钟

项目通用内容

通用返回类

  1. 先定义通用返回类
 /**
  * @program: miaosha
  * @description: 公共返回对象
  * @author: max-qaq
  * @create: 2022-03-20 21:24
  **/
 @Data
 @NoArgsConstructor
 @AllArgsConstructor
 public class RespBean {
     private long code;
     private String message;
     private Object obj;
 ​
     public static RespBean success(){
         return new RespBean(RespBeanEnum.SUCCESS.getCode(), RespBeanEnum.SUCCESS.getMessage(), null);
     }
 ​
     public static RespBean success(Object obj){
         return new RespBean(RespBeanEnum.SUCCESS.getCode(), RespBeanEnum.SUCCESS.getMessage(), obj);
     }
 ​
     public static RespBean error(RespBeanEnum respBeanEnum){
         return new RespBean(respBeanEnum.ERROR.getCode(), respBeanEnum.ERROR.getMessage(), null);
     }
 ​
     public static RespBean error(RespBeanEnum respBeanEnum , Object obj){
         return new RespBean(respBeanEnum.ERROR.getCode(), respBeanEnum.ERROR.getMessage(), obj);
     }
 }
  1. 定义通用返回枚举类
 @Getter
 @ToString
 @AllArgsConstructor
 public enum RespBeanEnum {
     //通用
     SUCCESS(200,"成功"),
     ERROR(500,"服务端异常"),
     //登录模块
     LOGIN_ERROR(500210,"用户名或密码错误"),
     MOBILE_ERROR(500220,"手机号格式错误"),
     BIND_ERROR(500230,"参数校验异常")
     ;
     private final Integer code;
     private final String message;
 }

自定义验证规则

  1. 创建自定义注解类
 @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Constraint(
         //校验规则
         validatedBy = { IsMobieValidator.class }
 )
 public @interface IsMobile {
 ​
     boolean required() default true;//必填
 ​
     String message() default "手机号码格式错误";//报错消息
 ​
     Class<?>[] groups() default {};
 ​
     Class<? extends Payload>[] payload() default {};
 }
  1. 创建校验规则类
 public class IsMobieValidator implements ConstraintValidator<IsMobile , String> {
 //ConstraintValidator的IsMobile是要验证定义规则的类
     private boolean required  = false;
 ​
     @Override
     public void initialize(IsMobile constraintAnnotation) {
         //初始化
         required = constraintAnnotation.required();
     }
 ​
     @Override
     public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
         if (required){
             //ValidatorUtil是定义了验证的工具类,就是检测字符串是否符合规则
             return ValidatorUtil.isMobile(s);
         }else{
             if(StringUtils.isEmpty(s)){
                 return true;
             }else{
                 ValidatorUtil.isMobile(s);
             }
         }
         return false;
     }
 }
 public class ValidatorUtil {
     //手机号
     private static final Pattern mobile_pattern = Pattern.compile("^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$");
 ​
     public static boolean isMobile(String mobile){
         //校验手机号
         if(StringUtils.isEmpty(mobile)) return false;
         Matcher matcher = mobile_pattern.matcher(mobile);
         return matcher.matches();
     }
 }

全局异常处理

  1. 定义全局异常
 @Data
 @NoArgsConstructor
 @AllArgsConstructor
 public class GlobalException extends RuntimeException{
     private RespBeanEnum respBeanEnum;//构造传入一个异常的枚举
 }
  1. 定义全局异常处理类
 /**
  * @program: miaosha
  * @description:全局异常处理类
  * @author: max-qaq
  * @create: 2022-04-01 13:02
  **/
 @RestControllerAdvice
 public class GlobalExceptionHandler {
 ​
     @ExceptionHandler(Exception.class)
     public RespBean ExceptionHandler(Exception e){
         if (e instanceof GlobalException){
             GlobalException ex = (GlobalException) e;
             return RespBean.error(ex.getRespBeanEnum());
         }else if (e instanceof BindException){
             BindException ex = (BindException) e;
             RespBean error = RespBean.error(RespBeanEnum.BIND_ERROR);
             error.setMessage("参数绑定异常: " + ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
             return error;
         }
         return RespBean.error(RespBeanEnum.ERROR);
     }
 }
  1. 使用的时候直接throw globalException
  if(null == user){
             throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
  }