您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“springboot啟動(dòng)項(xiàng)目打印接口列表的實(shí)現(xiàn)方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
springboot 啟動(dòng)項(xiàng)目打印接口列表
環(huán)境
修改配置文件
Springboot項(xiàng)目添加接口入?yún)⒔y(tǒng)一打印
新建注解,用于實(shí)現(xiàn)參數(shù)打印功能的增強(qiáng)
自定義序列化規(guī)則
寫參數(shù)打印增強(qiáng),這里選擇環(huán)繞增強(qiáng)
springboot 2.3.2.RELEASE
logging: level: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping: trace
結(jié)果:
需求:要求接口被調(diào)用時(shí)要打印被調(diào)用方法名,以及入?yún)⑶闆r,參數(shù)格式化時(shí)選擇fastjson
注:使用fastjson序列化時(shí)脫敏,建議入?yún)⒔y(tǒng)一使用自定義的對(duì)象類型作為入?yún)?/p>
如果不需要參數(shù)脫敏,直接使用增強(qiáng)中相關(guān)代碼,并去除參數(shù)脫敏相關(guān)代碼即可
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ParamInfo { /** * 取消統(tǒng)一打印參數(shù) * 默認(rèn)為false統(tǒng)一打印 * 如需自定義參數(shù)打印 請(qǐng)賦值為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í)格式化為 * 號(hào) */ public ReplaceFieldFilter(String... fields) { String str = "******"; fieldMap = new HashMap<>(4); for (String field : fields) { fieldMap.put(field, str); } } }
@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(); // 獲取方法對(duì)象 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); //通過(guò)注解獲取脫敏字段,之后初始化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)⒄蠟閷?duì)象,單個(gè)對(duì)象入?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); } } }
“springboot啟動(dòng)項(xiàng)目打印接口列表的實(shí)現(xiàn)方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。