zoukankan      html  css  js  c++  java
  • spring controller全局校验(JSR-303)、异常拦截AOP

    package dhht.seal.extension.config;

    import dhht.seal.extension.vo.ResultVO;
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    import org.springframework.validation.BindingResult;
    import org.springframework.validation.ObjectError;

    import java.util.List;

    /**
    * @Author: sh
    * @Description:
    * @Version:
    * @Date: 2020/4/4
    */
    @Slf4j
    @Component
    @Aspect
    public class ControllerResultAop {
       // 此包及其子包下的所有以Controller名称结尾的类下的所有方法
    @Pointcut("execution(* dhht.seal.extension.miniprogram..*Controller.*(..))")
    public void controllerPointCut() {
    }

    @Around(value = "controllerPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) {
    BindingResult bindingResult = null;
    try {
    Object[] args = joinPoint.getArgs();
    if (null != args && args.length > 0) {
    for (Object arg : args) {
    if (arg instanceof BindingResult) {
    bindingResult = (BindingResult) arg;
    break;
    }
    }
    }
    if (bindingResult != null) {
    List<ObjectError> errors = bindingResult.getAllErrors();
    if (errors.size() > 0) {
    for (ObjectError error : errors) {
    return ResultVO.fail(error.getDefaultMessage(),null);
    }
    }
    }
    return joinPoint.proceed();
    } catch (Throwable throwable) {
    String className = joinPoint.getTarget().getClass().getName();
    Signature signature = joinPoint.getSignature();
    String methodName = signature.getName();
    return ResultVO.fail(className+methodName+"执行失败",null);
    }
    }
    }
  • 相关阅读:
    检查使用的端口
    time is always agains us
    检查使用的端口
    dreque问题一例
    查看重定向的输出
    安装VSS时,Um.dat may be corrupt
    修改网卡ip
    redis install on ubuntu/debian
    上火了
    学这么多技术是为什么
  • 原文地址:https://www.cnblogs.com/sung1024/p/12631153.html
Copyright © 2011-2022 走看看