All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.leftins.tools.aspect.ValidationParam Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
package com.leftins.tools.aspect;

import com.alibaba.fastjson.JSONObject;

import com.leftins.tools.exception.ParamJsonException;
import com.leftins.tools.tools.ComUtil;
import com.leftins.tools.tools.StringUtil;
import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.Method;

/**
 * @author liugh
 * @since on 2018/5/10.
 */
public class ValidationParam implements AspectInterface{
    @Override
    public Object doHandlerAspect(Object [] obj ,ProceedingJoinPoint pjp, Method method,boolean isAll) throws Throwable{
       //获取注解的value值返回
        String validationParamValue = StringUtil.getMethodAnnotationOne(method,ValidationParam.class.getSimpleName());
        if(!ComUtil.isEmpty(validationParamValue)){
            for (int i = 0; i < obj.length; i++) {
                if(obj[i] instanceof JSONObject){
                    JSONObject jsonObject = JSONObject.parseObject(obj[i].toString());
                    //是否有所有必须参数
                    hasAllRequired(jsonObject,validationParamValue);
                }else {
                    continue;
                }
            }
        }
        return obj;
    }


    /**
     * 验证前端传入参数,没有抛出异常
     * @param jsonObject 参数对象实体
     * @param requiredColumns 需要校验的列
     */
    public void hasAllRequired(final JSONObject jsonObject, String requiredColumns) {
        if (!ComUtil.isEmpty(requiredColumns)) {
            //验证字段非空
            String[] columns = requiredColumns.split(",");
            String missCol = "";
            for (String column : columns) {
                Object val = jsonObject.get(column.trim());
                if (ComUtil.isEmpty(val)) {
                    missCol += column + "  ";
                }
            }
            if (!ComUtil.isEmpty(missCol)) {
                jsonObject.clear();
                throw new ParamJsonException("缺少必填参数:"+missCol.trim());
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy