1.依存性を追加します。
春ブーツでプロジェクトを作成した後、以下の依存性を追加し、それ以外のプロジェクトは、コメント欄を使用することはできません、我々はメソッド開発を傍受することはできません
<依存性> <のgroupId> org.springframework.boot </のgroupId> <たartifactId>ばねブートスタータAOP </たartifactId> </依存
2.カスタム注釈クラスLogRequest
/ ** * @ ClassNameLogRequest.java * @author Shengwu * @date 2019年10月19日 、このターゲットの役割* @Target注釈カッコ方法でこの注釈はのみに適用される上記の方法の意味を説明することができ @Retention注釈*予約された場所を反射のために注釈が実行時に存在してもよい説明ランタイム・ブラケットを使用することができることを意味 注釈の* @Documented説明は、Javadocの中に含まれる * / @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented パブリック @のインタフェース{LogRequest / ** *もしログテーブル * @return * / 文字列logFlag()デフォルトの "N" ; / ** *インターフェース名 * @return * / 文字列インタフェース名()デフォルト「」; }
3.カットクラス
輸入org.aspectj.lang.JoinPoint。 輸入org.aspectj.lang.ProceedingJoinPoint。 輸入org.aspectj.lang.annotation.Around; 輸入org.aspectj.lang.annotation.Aspect; 輸入org.slf4j.Logger。 輸入org.slf4j.LoggerFactory; 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.stereotype.Component。 輸入org.springframework.web.context.request.RequestContextHolder。 輸入org.springframework.web.context.request.ServletRequestAttributes。 インポートのjavax.servlet.http.HttpServletRequest; / ** *、主な役割は、要求インタフェースインターセプトするように切断クラスです *インターセプト方法を、唯一の上記の方法に指定されたインターフェイス@LogRequest注釈を追加する必要がある * * @author Shengwu * @version 1.0 * @since 2019 / 9月28日 * / @Aspect @Component パブリック クラスRequestAspect { プライベート 静的 最終ロガーロガー= LoggerFactory.getLogger(RequestAspect。クラス); @Autowired プライベートiInterfaceLogService iInterfaceLogService; / ** *修飾方法注釈付き@LogRequest前に実行されることになる示し実行doBefore()メソッド * * @paramジョインポイント连接点、就是被拦截点 * / @Around(値 = "@Annotation(LogRequest)" ) パブリックオブジェクトdoAround(ProceedingJoinPointジョインポイント、LogRequest LogRequest)がスローされたThrowable { ロング開始 = にSystem.currentTimeMillisを(); 試す{ オブジェクト結果 = joinPoint.proceed()。 この .methodAround(ジョインポイント、結果、開始、LogRequest.logFlag()、LogRequest.interfaceName()、真)。 戻り値の結果; } キャッチ(Throwableをスロー可能){ throwable.printStackTrace()。 この .methodAround(ジョインポイント、あとでThrowable.getMessage()、開始、LogRequest.logFlag()、LogRequest.interfaceName()、偽); スロースロー可能。 } } }
この方法4.Log
/ ** * * @param ジョインポイント * @paramのオブジェクトは、オブジェクトの値を返します * @param たstartTimeスタート時間法 * @param logFlagログが挿入されている * @param RequestStatusこのリクエストのステータス * * / パブリック 無効methodAround(ジョインポイントジョインポイント、オブジェクトオブジェクト、 startTimeのロング、logFlag文字列、文字列インタフェース名、ブールRequestStatus){ // 取得要求された属性 ServletRequestAttributes属性= (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); //はにリクエストオブジェクトを取得します。 = HttpServletRequestのリクエストattributes.getRequest(); // URLを:要求されたオブジェクトのアドレスへのアクセスを取得 logger.infoを( "URL = {}" 、request.getRequestURL()); logger.info( "インタフェース名= {}" 、インタフェース名); logger.info( "プロキシがオブジェクト自体:{}" + joinPoint.getThis()); // 受信対象プロセスパラメータ取得 オブジェクト[]引数の=のjoinPoint.getArgsを(); 長時間 =(システム。 currentTimeMillis()と- のstartTime); logger.info( "実行時間} {" 、時間); 長いendTimeは = にSystem.currentTimeMillis(); 日付 = 新しい新日付(のstartTime)。 InterfaceLogDTO interfaceLogDTO = 新しいInterfaceLogDTO(); 文字列UUID = 。UUID.randomUUID()のtoString(); interfaceLogDTO.setInvokeKey(UUID)。 // 微服务应用名 interfaceLogDTO.setServerCode(serverCode)。 interfaceLogDTO.setRequestTime(日付); interfaceLogDTO.setInterfaceResponseTime(endTimeの - のstartTime)。 interfaceLogDTO.setServerName(インタフェース名)。 interfaceLogDTO.setApplicationCode(applicationInitListener.getServerIpPort())。 interfaceLogDTO.setUserAgent(request.getHeader( "ユーザーエージェント"))。 CustomUserDetails CustomClientは = DetailsHelper.getUserDetails()。 もし(CustomClientは== NULL || customClient.getOrganizationId()== NULL ){ logger.error( "接口" +インタフェース名+ "租户获取空" )。 返します。 } interfaceLogDTO.setTenantId(customClient.getOrganizationId())。 interfaceLogDTO.setClientId(customClient.getClientId() == nullの?ナル:String.valueOf(customClient.getClientId())); interfaceLogDTO.setInterfaceUrl(request.getRequestURL()のtoString())。 interfaceLogDTO.setRequestMethod(request.getMethod())。 interfaceLogDTO.setIp(request.getRemoteAddr())。 interfaceLogDTO.setInterfaceRequestTime(日付); // 明细 InterfaceLogDtlDTO interfaceLogDtlDTO = 新しいInterfaceLogDtlDTO(); interfaceLogDTO.setInterfaceLogDtlDTO(interfaceLogDtlDTO)。 interfaceLogDtlDTO.setInvokeKey(UUID)。 StringBuilderのStringBuilderの = 新しいStringBuilderの(); 以下のために(int型 i = 0; I <args.length; iは++ ){ 試みる{ stringBuilder.append(objectMapper.writeValueAsString(引数を[I]))。 } キャッチ(JsonProcessingException E){ e.printStackTrace(); } } interfaceLogDtlDTO.setInterfaceReqBodyParam(stringBuilder.toString())。 文字列interfaceRespContent = nullを。 してみてください{ 場合(結果=!ヌル){ // 返回内容 interfaceRespContent = objectMapper.writeValueAsString(結果); } } キャッチ(JsonProcessingException電子){ e.printStackTrace(); } であれば(requestStatus){ interfaceLogDtlDTO.setInterfaceRespContent(interfaceRespContent)。 interfaceLogDTO.setResponseStatus(成功)。 interfaceLogDTO.setInterfaceResponseStatus(成功)。 } もし(!{requestStatus) interfaceLogDtlDTO.setStacktrace(interfaceRespContent)。 interfaceLogDTO.setResponseStatus(FAIL)。 interfaceLogDTO.setInterfaceResponseStatus(FAIL)。 } もし(!FLAG_Y.equals(logFlag)){ logger.info( "出站信息{}" 、interfaceLogDTO.toString())。 返します。 } }