美文网首页
SSM框架 基于spring-AOP 实现requet参数日志记

SSM框架 基于spring-AOP 实现requet参数日志记

作者: CatalpaFlat | 来源:发表于2017-11-13 11:00 被阅读68次

    SSM框架 基于spring-AOP 实现requet参数日志记录

    1.参数类型

    1. from表单提交,可以从request的getParameterMap()获取
    2. raw提交(对象类型提交),则得从request的getReade()或getInputStream()

    2.添加maven依赖

    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.4</version>
      <classifier>jdk15</classifier>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>${gson.version}</version>
    </dependency>
    

    3.配置aop切面类RequsetParamsJouranAspect

    /**
     * request 参数日志记录
     * @Author: CatalpaFlat
     * @Descrition:
     * @Date: Create in 22:24 2017/11/12
     * @Modified BY:
     */
    @Aspect
    @Component
    public class RequsetParamsJouranAspect {
        /**
         * service层切面
         */
        private final String POINT_CUT = "execution(* com.chen.logic.controller..*(..))";
        /**
         * 日志输出
         */
        private static final Logger logger = Logger.getLogger(RequsetParamsJouranAspect.class.getName());
    
    
        @Pointcut(POINT_CUT)
        private void pointcut() {
        }
    
        @Before(value = "pointcut()")
        public void before(JoinPoint joinPoint) {
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes sra = (ServletRequestAttributes) requestAttributes;
            HttpServletRequest request = sra.getRequest();
    
            Map<String, Object> returnMap = new HashMap<>();
            Map<String, String[]> parameterMap = request.getParameterMap();
            Iterator entries = parameterMap.entrySet().iterator();
            Map.Entry entry;
            String name;
            String value = "";
            while (entries.hasNext()) {
                entry = (Map.Entry) entries.next();
                name = (String) entry.getKey();
                Object valueObj = entry.getValue();
                if (null == valueObj) {
                    value = "";
                } else if (valueObj instanceof String[]) {
                    String[] values = (String[]) valueObj;
                    for (String v : values) {
                        value = v + ",";
                    }
                    value = value.substring(0, value.length() - 1);
                } else {
                    value = valueObj.toString();
                }
                returnMap.put(name, value);
            }
    
            JSONObject paramsJson = JSONObject.fromObject(new Gson().toJson(returnMap));
            logger.info("paramsJson:"+paramsJson.toString());
    
            Object[] args = joinPoint.getArgs();
            JSONArray bodyArray = new JSONArray();
            for (Object obj:args){
                if (!(obj instanceof HttpServletRequest)&&!isBasicDataType(obj))
                    bodyArray.add(JSONObject.fromObject(obj));
            }
            logger.info("bodyArray:"+bodyArray.toString());
        }
    
        /**
         * 是否为基本数据类型还是自定义对象
         * @param obj 参数对象
         * @return 是否为基本数据类型还是自定义对象(true/false)
         */
        private boolean isBasicDataType(Object obj){
            if (obj instanceof String)
                return true;
            if (obj instanceof Boolean)
                return true;
            Class<?> aClass = obj.getClass();
            return aClass.isPrimitive();
        }
    }
    

    注:

    • 当以@RequestBody注解对象时候,以raw格式提交参数,以对象接收,因此只要判断其是否为基本数据类型即可排除参数类型

    4.测试

    基于:http://blog.csdn.net/dushiwodecuo/article/details/78507296

    4.1.Controller

    @PostMapping(value = "getraw")
    public String getraw(@RequestBody Test test){
        return "OK";
    }
    
    @PostMapping(value = "getParams")
    public String getParams(@RequestParam String name){
        return name;
    }
    

    4.2.测试调用

    image
    image
    image
    image

    相关文章

      网友评论

          本文标题:SSM框架 基于spring-AOP 实现requet参数日志记

          本文链接:https://www.haomeiwen.com/subject/yinamxtx.html