Spring framework框架为我们提供了很多有用的工具,比如Validation类用于处于HTTP请求的参数校验。
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
我们可以在Controller层获取Error来判断是否校验失败,但是这样每一个Controller层的方法都有重复的代码,我们可以实现统一的切面Aspect去为所有的方法都添加 校验逻辑。
@Aspect是Spring面向切面编程最重要的注解~可以实现在方法执行的时候添加额外的行为, 包括方法执行前Before、方法执行后After、方法抛出异常Exception、方法环绕Around等等时机。
import com.nova.felixchat.infrastructure.commons.BusinessException;
import com.nova.felixchat.infrastructure.commons.enums.BizErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springf