5 回答

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;
}
}

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,因為該屬性直到請求生命周期的后期才會設置。

TA貢獻1872條經驗 獲得超4個贊
你可以試試這個:
String path = (String)request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

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

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
程序化方法
如果你想實現這個如果你想以編程方式實現這個。
@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"
? ? ? ? ? ? ]
? ? ? ? }
? ? ]
}
添加回答
舉報