项目通用内容
通用返回类
- 先定义通用返回类
@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);
}
}
- 定义通用返回枚举类
@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;
}
自定义验证规则
- 创建自定义注解类
@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 {};
}
- 创建校验规则类
public class IsMobieValidator implements ConstraintValidator<IsMobile , String> {
private boolean required = false;
@Override
public void initialize(IsMobile constraintAnnotation) {
required = constraintAnnotation.required();
}
@Override
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
if (required){
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();
}
}
全局异常处理
- 定义全局异常
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GlobalException extends RuntimeException{
private RespBeanEnum respBeanEnum;
}
- 定义全局异常处理类
@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);
}
}
- 使用的时候直接throw globalException
if(null == user){
throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
}