亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取已命中的URL模板

獲取已命中的URL模板

嚕嚕噠 2023-04-26 16:16:17
我有幾個 API 的 spring MVC 控制器,我想獲取從前端命中的 API 的 URI我有一個過濾器擴展 oncePerRequestFilter,它攔截每個 api 調用,過濾器有接受 HttpServletRequest、HttpServletResponse 和 filterChain 的方法。我可以使用 urirequest.getRequestURI()但如果 uri 具有模板路徑變量,可以說 uri ="q/v1/ruleset/{rulesetId}" 我正在獲取最終的 uri,例如 API 已被命中rulesetid=23,我從中獲取的 urirequest.getRequestURI()是 "q/v1/ruleset/23"但我想要的是uri ="q/v1/ruleset/{rulesetId}",有沒有任何方法可以得到預期的結果,我知道,我總能通過一些操作得到想要的結果,但我想讓事情變得通用,請幫忙我有包含 API 的控制器    @RequestMapping(value = "/ruleset/{rulesetid}", method =               RequestMethod.GET)    public RuleSet getRuleSet(@PathVariable(value = "rulesetid")     final Long ruleSetId) {         return storeMixin.getRuleSet(ruleSetId);    }篩選    @Component    @Order(1)    public class CatalogFilter extends OncePerRequestFilter {    @Override    protected void doFilterInternal(HttpServletRequest request,     HttpServletResponse response,FilterChain filterChain) throws       IOException, ServletException {         long startTime = System.currentTimeMillis();         filterChain.doFilter(request, response);         long elapsed = System.currentTimeMillis() - startTime;         String name = request.getRequestURI();         String requestType = request.getMethod();         Integer httpCode = response.getStatus();      }    }
查看完整描述

5 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

您將無法在過濾器中執行此操作,因為過濾器是在處理程序之前執行的。


您可以實現一個 HandlerInterceptor 并獲得如下所示的路徑映射


public class LogInterceptor implements HandlerInterceptor {


     @Override

      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {

          String path = (String)request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

          System.out.println("path : " + path);

        return true;

      }

}


查看完整回答
反對 回復 2023-04-26
?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

做這個


    @Override

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)

            throws ServletException, IOException {

        try {

            filterChain.doFilter(request, response);

        } finally {

            String patternMatch = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

        }

    }


在嘗試調用 request.getAttribute 之前,您需要先執行 filterChain.doFilter,因為該屬性直到請求生命周期的后期才會設置。


查看完整回答
反對 回復 2023-04-26
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

你可以試試這個:

String path = (String)request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);



查看完整回答
反對 回復 2023-04-26
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

我懷疑您能否通過現有 API 將其放入過濾器中。但是,您可以預掃描所有 @RequestMapping 注釋,保留它,然后在過濾器中匹配模式以獲得所需的結果。



查看完整回答
反對 回復 2023-04-26
?
holdtom

TA貢獻1805條經驗 獲得超10個贊

Spring Boot Admin - 易于使用 GUI

另一種方法是使用Spring Boot Admin。為此,我們必須配置客戶端-服務器。為避免錯誤,請確??蛻舳?服務器依賴項的版本相同。我們可以從下拉列表中添加所需的指標,如圖所示。


您可以將 uri:/user/getEmployee/{employeeId} 的 TOTAL_TIME 計數為 3, 0.2264027


客戶端:

pom.xml


<dependency>

? ? ?<groupId>de.codecentric</groupId>

? ? ?<artifactId>spring-boot-admin-starter-client</artifactId>

? ? ?<version>2.1.4</version>

</dependency>

application.properties


spring.boot.admin.api-path=/instances

spring.boot.admin.client.url=http://localhost:6699

management.endpoints.web.exposure.include=*

服務器端:

application.properties


server.port = 6699

spring.boot.admin.server.url=http://localhost:8889

pom.xml


?<dependency>

? ? ? ? ?<groupId>de.codecentric</groupId>

? ? ? ? ?<artifactId>spring-boot-admin-starter-server</artifactId>

? ? ? ? ?<version>2.1.4</version>

? ? </dependency>

添加@EnableAdminServer


import de.codecentric.boot.admin.server.config.EnableAdminServer;


@SpringBootApplication

@EnableAdminServer

public class AdminApplication {


? ? public static void main(String[] args) {

? ? ? ? SpringApplication.run(AdminApplication.class, args);

? ? }


}

界面?http://localhost:6699/#/applications

http://img1.sycdn.imooc.com/6448de3c0001421e06550157.jpg


程序化方法

如果你想實現這個如果你想以編程方式實現這個。

@RequestMapping(path="/admin/count",method=RequestMethod.POST)

public JSONObject count(@RequestParam(name="url") final String url)//@PathVariable(name="url") final String url

{? ?

? ? String finalURL = "http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:" + url + "";

? ? return sendRequestToURL(finalURL);??

}

我們可以使用 Spring Boot/actuator/metrics/http.server.requests獲取所有執行的端點及其計數、異常、結果、狀態、總時間等,如下所示。


如果您想查看特定端點的詳細信息,則可以通過調用請求來完成,如下所示


localhost:8889/actuator/metrics/http.server.requests?tag=uri:<endPoint>

localhost:8889/actuator/metrics/http.server.requests?tag=uri:/user/asset/getAllAssets

localhost:8889/actuator/metrics/http.server.requests?tag=uri:/user/asset/getAllAssets&tag=status:200

您將獲得COUNT特定端點被調用的次數

您將獲得具有特定狀態的特定端點被 調用的COUNT次數

要獲得執行 endPoint 的平均時間,您可以 TOTAL_TIME/COUNT為特定的 endPoint 以及整個應用程序執行

更詳細的解釋

本地主機:8889/actuator/metrics/http.server.requests


{

? ? "name": "http.server.requests",

? ? "description": null,

? ? "baseUnit": "seconds",

? ? "measurements": [

? ? ? ? {

? ? ? ? ? ? "statistic": "COUNT",

? ? ? ? ? ? "value": 3

? ? ? ? },

? ? ? ? {

? ? ? ? ? ? "statistic": "TOTAL_TIME",

? ? ? ? ? ? "value": 0.21817219999999998

? ? ? ? },

? ? ? ? {

? ? ? ? ? ? "statistic": "MAX",

? ? ? ? ? ? "value": 0.1379249

? ? ? ? }

? ? ],

? ? "availableTags": [

? ? ? ? {

? ? ? ? ? ? "tag": "exception",

? ? ? ? ? ? "values": [

? ? ? ? ? ? ? ? "MethodArgumentTypeMismatchException",

? ? ? ? ? ? ? ? "None"

? ? ? ? ? ? ]

? ? ? ? },

? ? ? ? {

? ? ? ? ? ? "tag": "method",

? ? ? ? ? ? "values": [

? ? ? ? ? ? ? ? "GET"

? ? ? ? ? ? ]

? ? ? ? },

? ? ? ? {

? ? ? ? ? ? "tag": "uri",

? ? ? ? ? ? "values": [

? ? ? ? ? ? ? ? "/{id}.*",

? ? ? ? ? ? ? ? "/user/asset/getAsset/{assetId}",

? ? ? ? ? ? ? ? "/user/asset/getAllAssets"

? ? ? ? ? ? ]

? ? ? ? },

? ? ? ? {

? ? ? ? ? ? "tag": "outcome",

? ? ? ? ? ? "values": [

? ? ? ? ? ? ? ? "CLIENT_ERROR",

? ? ? ? ? ? ? ? "SUCCESS"

? ? ? ? ? ? ]

? ? ? ? },

? ? ? ? {

? ? ? ? ? ? "tag": "status",

? ? ? ? ? ? "values": [

? ? ? ? ? ? ? ? "400",

? ? ? ? ? ? ? ? "404",

? ? ? ? ? ? ? ? "200"

? ? ? ? ? ? ]

? ? ? ? }

? ? ]

}



查看完整回答
反對 回復 2023-04-26
  • 5 回答
  • 0 關注
  • 223 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號