springboot aop方式实现接口入参校验

一、前言
在实际开发项目中,我们常常需要对接口入参进行校验,如果直接在业务代码中进行校验,则会显得代码非常冗余,也不够优雅,那么我们可以使用aop的方式校验,这样则会显得更优雅。

二、如何实现?
1.添加maven依赖

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

2.定义一个工具类ValidationUtil

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class ValidationUtil {

    private static ValidationUtil util;

    private Validator validator;

    public ValidationUtil() {
        // TODO Auto-generated constructor stub
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        this.validator = factory.getValidator();
    }
/**
     * @return the validator
     */
    public Validator getValidator() {
        return validator;
    }

    public static ValidationUtil getValidationUtil() {
        if (util == null) {
            util = new ValidationUtil();
        }
        return util;

    }

    public static List<CheckErrorResultDto> validate(Object o) {
        Set<ConstraintViolation<Object>> set = ValidationUtil
                .getValidationUtil().getValidator().validate(o);
        List<CheckErrorResultDto> errorList = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();

        for (ConstraintViolation<Object> cv : set) {
            if (errorList == null) {
                errorList = new ArrayList<CheckErrorResultDto>();
            }
            String message = MessageResolver.getMessage(request,
                    cv.getMessage());
            errorList.add(new CheckErrorResultDto(cv.getPropertyPath()
                    .toString(), message));

        }
        return errorList;
    }
    public static List<CheckErrorResultDto> validate(Object o,Class<?> ...c) {
        Set<ConstraintViolation<Object>> set = ValidationUtil
                .getValidationUtil().getValidator().validate(o,c);
        List<CheckErrorResultDto> errorList = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();

        for (ConstraintViolation<Object> cv : set) {
            if (errorList == null) {
                errorList = new ArrayList<CheckErrorResultDto>();
            }
            String message = MessageResolver.getMessage(request,
                    cv.getMessage());
            errorList.add(new CheckErrorResultDto(cv.getPropertyPath()
                    .toString(), message));

        }
        return errorList;
    }

3.校验错误信息实体类。

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CheckErrorResultDto {

    private String fieldName;

    private String msg;
}

4.编写校验AOP

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@Order(1)
@Aspect
@Component
@EnableAspectJAutoProxy(exposeProxy=true)
public class ValidAop {


    /**
     * 校验传入实体
     *
     * @param pjp
     * @throws Throwable
     */
    @Around("@annotation(com.dp.aop.annotation.Valid)")
    public Object aroundExec(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature ms = (MethodSignature) pjp.getSignature();
        Method method = ms.getMethod();
        Valid annotation = method.getAnnotation(Valid.class);
        if (annotation != null) {
            List<CheckErrorResultDto> errorList=new ArrayList<CheckErrorResultDto>();
            Object[] args=pjp.getArgs();
            Class<?>[] checkToken = annotation.value();
            Class<?>[] group = annotation.group();
            for (Class c : checkToken) {
                for (Object obj : args) {
                    if (c == obj.getClass()) {
                        List<CheckErrorResultDto> result=null;
                        if(group.length<1){
                            result= ValidationUtil.validate(obj);
                        }else{
                            result= ValidationUtil.validate(obj,group);
                        }
                        if(result!=null)
                            errorList.addAll(result);
                    }

                }
            }
            if(errorList.size()>0){
                ResponseData vo=new ResponseData();
                vo.setCode(ApiCodeEnum.ILLEGAL_PARAMETER.getCode());
                vo.setMsg(ApiCodeEnum.ILLEGAL_PARAMETER.getMsg());
                vo.setData(errorList);
                return vo;
            }
        }
        return pjp.proceed();
    }
}

5.定义一个校验注解Valid

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Valid {

    Class<?>[] value() default {};
    Class<?>[] group() default {};
}

6.最后在Controller定义的接口上加上注解指定要校验的对象即可。

大家可以尝试下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜空下的星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值