• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

java springboot 操作日志通过joinPoint 获取request请求的JSON数据,实测有效

武飞扬头像
攀小黑
帮助1

这几天公司业务需求要弄一个操作日志,需要获取到用户请求的参数,在网上看了很多方法,在我这都没用,无奈看去别人写好的源码,总算找出来了。

因为要转化成JSON字符串,所以需要 用到JSON包,这里用的是阿里巴巴的:

  1.  
    <!-- 阿里JSON解析器 -->
  2.  
    <dependency>
  3.  
    <groupId>com.alibaba.fastjson2</groupId>
  4.  
    <artifactId>fastjson2</artifactId>
  5.  
    <version>2.0.26</version>
  6.  
    </dependency>

废话不多说,直接上源码(帮到各位请点赞支持一下吧):

  1.  
    /**
  2.  
    * 通过JoinPoint 获取请求参数JSON字符串并返回
  3.  
    */
  4.  
    private String argsArrayToString(JoinPoint joinPoint)
  5.  
    {
  6.  
     
  7.  
    Object[] paramsArray = joinPoint.getArgs();
  8.  
    String params = "";
  9.  
    if (paramsArray != null && paramsArray.length > 0)
  10.  
    {
  11.  
    for (Object o : paramsArray)
  12.  
    {
  13.  
    if (StringUtils.isNotNull(o) && !isFilterObject(o))
  14.  
    {
  15.  
    try
  16.  
    {
  17.  
    String jsonObj = JSON.toJSONString(o);
  18.  
    params = jsonObj.toString() " ";
  19.  
    }
  20.  
    catch (Exception e)
  21.  
    {
  22.  
    }
  23.  
    }
  24.  
    }
  25.  
    }
  26.  
    return params.trim();
  27.  
    }
  28.  
     
  29.  
     
  30.  
     
  31.  
    /**
  32.  
    * 忽略敏感属性
  33.  
    */
  34.  
    public PropertyPreExcludeFilter excludePropertyPreFilter(String[] excludeParamNames)
  35.  
    {
  36.  
    return new PropertyPreExcludeFilter().addExcludes(ArrayUtils.addAll(PropertyPreExcludeFilter.EXCLUDE_PROPERTIES, excludeParamNames));
  37.  
    }
  38.  
     
  39.  
    /**
  40.  
    * 判断是否需要过滤的对象。
  41.  
    *
  42.  
    * @param o 对象信息。
  43.  
    * @return 如果是需要过滤的对象,则返回true;否则返回false。
  44.  
    */
  45.  
    @SuppressWarnings("rawtypes")
  46.  
    public boolean isFilterObject(final Object o)
  47.  
    {
  48.  
    Class<?> clazz = o.getClass();
  49.  
    if (clazz.isArray())
  50.  
    {
  51.  
    return clazz.getComponentType().isAssignableFrom(MultipartFile.class);
  52.  
    }
  53.  
    else if (Collection.class.isAssignableFrom(clazz))
  54.  
    {
  55.  
    Collection collection = (Collection) o;
  56.  
    for (Object value : collection)
  57.  
    {
  58.  
    return value instanceof MultipartFile;
  59.  
    }
  60.  
    }
  61.  
    else if (Map.class.isAssignableFrom(clazz))
  62.  
    {
  63.  
    Map map = (Map) o;
  64.  
    for (Object value : map.entrySet())
  65.  
    {
  66.  
    Map.Entry entry = (Map.Entry) value;
  67.  
    return entry.getValue() instanceof MultipartFile;
  68.  
    }
  69.  
    }
  70.  
    return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
  71.  
    || o instanceof BindingResult;
  72.  
    }
学新通

其中 isFilterObject 方法用来过滤掉不用的参数,比如用户的密码等,根据业务需求可以删除。

拿到的数据样子如下:

{"id":1,"level":50,"name":"决热快","pageIndex":10,"pageSize":1,"pageSorts":[],"parentId":75,"sort":49,"type":37}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgihiif
系列文章
更多 icon
同类精品
更多 icon
继续加载