自定义注解
项目开放接口的时候通常需要对接口入参进行一定的校验,此文章拿最常见的非空验证进行举例。
大致的原理如下:定义一个自定义注解,对自定义注解进行拦截进行校验,不满足条件抛出自定义异常,捕获自定义异常进行处理并返回错误信息给调用者。
首先定义一个Dto进行入参的接收
public class WingzingDto implements Serializable {
private String userId;
private String userName;
//get set
}
配置一个自定义注解类
/**
* 接口参数非空校验注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ApiParamValidate {
String value();
}
然后编写一个切面类,对此注解进行拦截。
/**
* 接口入参非空验证,切面处理类
*
*/
@Aspect
public class ApiParamValidateAspect {
@Pointcut("@annotation(com.xx.xx.ApiParamValidate)")
public void apiParamValidatePointCut() {
}
@Around("apiParamValidatePointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
ApiParamValidate apiParamValidate = method.getAnnotation(ApiParamValidate.class);
if (apiParamValidate != null) {
String requestParam = apiParamValidate.value();
if(requestParam.length()>0){
String [] paramArray = requestParam.split(",");//注解配置的所需校验参数
Object[] paramValues = point.getArgs();//api接口定义的参数
String[] paramValuesNames = signature.getParameterNames();//api接口定义的参数名称
if(paramValues.length>0){
for(String needParamName : paramArray){
validate(needParamName,paramValues,paramValuesNames);
}
}else{
//参数为空
throw new RRException("参数与api接口不匹配!");
}
}else{
throw new RRException("api接口若不需要校验参数,请不要使用此注解!");
}
}
return point.proceed();
}
private static boolean isJavaClass(Class<?> clz) {
return clz != null && clz.getClassLoader() == null;
}
private static void validate(String needParamName,Object[] objs,String[] objNames){
if(objs!=null&&objs.length>0){
boolean b = false;
for(int i=0;i<objs.length;i++){
if(objs[i]!=null){
if(!isJavaClass(objs[i].getClass())){//参数是自定义对象
//自定义类
Class clz = objs[i].getClass();
try{
Method mt = clz.getMethod("get"+needParamName.substring(0,1).toUpperCase()+needParamName.substring(1));
Object ret = mt.invoke(objs[i]);
if(ret==null||ret.toString().equals("")){
throw new RRException("参数 "+needParamName+" 为空");
}else{
b = true;//在参数中找到了必填参数,且值不为空
}
}catch (NoSuchMethodException e){
System.out.println("没有在此对象中找到参数 "+needParamName+" 的getter setter方法");
}catch(Exception e){
System.out.println("没有在此对象中找到参数 "+needParamName+" 的getter setter方法");
}
}else{//参数是系统类
if(objNames[i].equals(needParamName)){
if(objs[i]==null||objs[i].toString().equals("")){
throw new RRException("参数 "+needParamName+" 为空");
}else{
//在参数中找到了必填参数,且值不为空
b = true;
}
}
}
}
}
if(!b){
//没有找到必填参数
throw new RRException("参数 "+needParamName+" 为空");
}
}else{
throw new RRException("api接口入参未定义!请联系管理员!");
}
}
}
@Aspect 用来标识此类是一个切面供容器进行读取@Pointcut 切点 用于标识什么时候进行拦截,后面的@annotation标识了具体拦截的类 即我定义的自定注解@Around 环绕增强,与@AfterReturning,@Before,@AfterThrowing,@After等等类似。
这里实现的效果就是对自定义注解进行拦截,然后在 around方法中进行了参数循环调用validate方法,通过反射的方式进行调用get方法,然后判断是否为空(这里也可以进行各种各样的校验)。
我选择抛出自定义的异常RRException,来提示调用接口方错误信息。
附上RRException,以及对应的捕获该异常的类。
/**
* 自定义异常
*
*/
public class RRException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String msg;
private int code = 500;
public RRException(String msg) {
super(msg);
this.msg = msg;
}
public RRException(String msg, Throwable e) {
super(msg, e);
this.msg = msg;
}
public RRException(String msg, int code) {
super(msg);
this.msg = msg;
this.code = code;
}
public RRException(String msg, int code, Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
/**
* 异常处理器
*
*/
@ControllerAdvice
public class RRExceptionHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 处理自定义异常
*/
@ExceptionHandler(RRException.class)
@ResponseBody
public String handleRRException(RRException e){
logger.debug(e.getMessage(), e);
System.out.println(e.toString());
return BackResult.error(e.getMsg()).toJson();
}
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseBody
public String handlerNoFoundException(NoHandlerFoundException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
return BackResult.error("路径不存在,请检查路径是否正确").toJson();
}
@ExceptionHandler(Exception.class)
@ResponseBody
public String handleException(Exception e){
logger.error(e.getMessage(), e);
e.printStackTrace();
return BackResult.error("未知异常,请联系管理员").toJson();
}
}
@ControllerAdvice 增强型controller,使用此注解可以使得整个项目的异常都可以被此捕获。@ExceptionHandler 异常捕获,后面的参数代表拦截什么类型的异常。
使用postman调用看一下大概的效果: