一、前言
在实际开发项目中,我们常常需要对接口入参进行校验,如果直接在业务代码中进行校验,则会显得代码非常冗余,也不够优雅,那么我们可以使用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定义的接口上加上注解指定要校验的对象即可。
大家可以尝试下。