最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot 啟動(dòng)項(xiàng)目打印接口列表的實(shí)現(xiàn)

 更新時(shí)間:2021年09月11日 08:44:50   作者:enjoy囂士  
這篇文章主要介紹了springboot 啟動(dòng)項(xiàng)目打印接口列表的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot 啟動(dòng)項(xiàng)目打印接口列表

環(huán)境

  • springboot 2.3.2.RELEASE

修改配置文件

logging:
  level:
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping: trace

結(jié)果:

在這里插入圖片描述

Springboot項(xiàng)目添加接口入?yún)⒔y(tǒng)一打印

需求:要求接口被調(diào)用時(shí)要打印被調(diào)用方法名,以及入?yún)⑶闆r,參數(shù)格式化時(shí)選擇fastjson

注:使用fastjson序列化時(shí)脫敏,建議入?yún)⒔y(tǒng)一使用自定義的對象類型作為入?yún)?/p>

如果不需要參數(shù)脫敏,直接使用增強(qiáng)中相關(guān)代碼,并去除參數(shù)脫敏相關(guān)代碼即可

新建注解,用于實(shí)現(xiàn)參數(shù)打印功能的增強(qiáng)

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ParamInfo {
    /**
     * 取消統(tǒng)一打印參數(shù)
     * 默認(rèn)為false統(tǒng)一打印
     * 如需自定義參數(shù)打印 請賦值為true
     */
    boolean unPrint() default false;
    /**
     * 需要脫敏的字段,如密碼等
     */
    String[] fields() default {};
}

自定義序列化規(guī)則

/**
 * 序列化過濾器:值替換
 *
 */
public class ReplaceFieldFilter implements ValueFilter {
    /**
     * 需要進(jìn)行替換的屬性名和替換值
     * key:屬性名
     * value:替換值
     */
    private Map<String, Object> fieldMap;
    public ReplaceFieldFilter() {
    }
    public ReplaceFieldFilter(Map<String, Object> fieldMap) {
        this.fieldMap = fieldMap;
    }
    @Override
    public Object process(Object o, String name, Object value) {
        if(!CollectionUtils.isEmpty(fieldMap)){
            Iterator<Map.Entry<String, Object>> iterator = fieldMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String, Object> next = iterator.next();
                if(next.getKey().equalsIgnoreCase(name)){
                    return next.getValue();
                }
            }
        }
        return value;
    }
    public Map<String, Object> getFieldMap() {
        return fieldMap;
    }
    public void setFieldMap(Map<String, Object> fieldMap) {
        this.fieldMap = fieldMap;
    }
    /**
     * 傳入需要脫敏的字段名,序列化時(shí)格式化為 * 號
     */
    public ReplaceFieldFilter(String... fields) {
        String str = "******";
        fieldMap = new HashMap<>(4);
        for (String field : fields) {
            fieldMap.put(field, str);
        }
    }
}

寫參數(shù)打印增強(qiáng),這里選擇環(huán)繞增強(qiáng)

@Component
@Aspect
//表示增強(qiáng)的執(zhí)行順序,如果多個(gè)增強(qiáng),數(shù)值小的先被執(zhí)行
@Order(0)
public class ParamInfoAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(ParamInfoAspect.class);
    @Around("execution(* com.service.impl.*.*(..))")
    public Object printParam(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        String requestId = RandomStringUtils.randomAlphanumeric(16);
        Object returnValue = null;
        try {
            Object[] args = joinPoint.getArgs();
            // 獲取方法對象
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            //通過注解獲取脫敏字段,之后初始化fieldMap,完成字段脫敏
            ParamInfo annotation = method.getAnnotation(ParamInfo.class);
            Map<String, Object> fieldMap = new HashMap<>(4);
            fieldMap.put("password", "******");
            if (annotation != null) {
                //獲取需要脫敏的字段名數(shù)組
                String[] fields = annotation.fields();
                for (String field : fields) {
                    fieldMap.put(field, "******");
                }
            }
            String param;
            //參數(shù)整合,多字段入?yún)⒄蠟閷ο?,單個(gè)對象入?yún)⒏袷讲蛔?
            if (args.length > 1 || (args.length == 1 && args[0].getClass() == String.class)) {
                Map<String, Object> paramMap = new LinkedHashMap<>();
                String[] parameterNames = signature.getParameterNames();
                for (int i = 0; i < parameterNames.length; i++) {
                    paramMap.put(parameterNames[i], args[i]);
                }
                param = "[" + JSON.toJSONString(paramMap, new ReplaceFieldFilter(fieldMap)) + "]";
            } else {
                param = JSON.toJSONString(args, new ReplaceFieldFilter(fieldMap));
            }
            String methodName = method.getName();
            LOGGER.info("method:[{}], parameter:{}, requestId:[{}]", methodName, param, requestId);
            returnValue = joinPoint.proceed();
            return returnValue;
        } catch (Exception e) {
            LOGGER.error("system is error:", e);
   //可在這里定義程序異常時(shí)的錯(cuò)誤返回值
            returnValue = ErrorCode.SYSTEM_ERROR;
            return returnValue;
        } finally {
            LOGGER.info("request cost:{}ms, requestId:[{}]", System.currentTimeMillis() - startTime, requestId);
            LOGGER.info("returnValue:[{}], requestId:[{}]", returnValue, requestId);
        }
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

衡阳市| 通江县| 神农架林区| 津市市| 来宾市| 新绛县| 庐江县| 涟源市| 桃源县| 宁化县| 岑巩县| 石景山区| 桐乡市| 昭平县| 庆元县| 大丰市| 宝山区| 霍州市| 延庆县| 公安县| 星座| 当雄县| 益阳市| 伽师县| 凭祥市| 买车| 兴仁县| 宁陵县| 紫阳县| 沂源县| 海城市| 宜城市| 内江市| 中阳县| 华安县| 金昌市| 宕昌县| 伊春市| 遵义县| 大宁县| 调兵山市|