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

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

彈簧靴:計數頁面瀏覽量 - 執行器

彈簧靴:計數頁面瀏覽量 - 執行器

守著一只汪 2022-09-01 19:32:39
我需要計算每個端點上的視圖。我們的想法是為所有端點創建一個通用的請求計數映射,該映射應基于動態輸入的終結點返回視圖計數。假設有人想要檢查 的視圖計數。http://localhost:8080/user/101請求映射path = /admin/count & RequestParam = url (Here /user/101)然后創建dynamic Request based on RequestParam http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101Get and Return the Response的動態請求并獲取的值(JSON Object)COUNT我堅持如何發送一個到 http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101 并返回它的響應并獲取計數值dynamic request@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);  }@RequestMapping(path="/{finalURL}",method=RequestMethod.GET)public JSONObject sendRequestToURL(@PathVariable("finalURL") String url){    //How to return the response Here}這就是我在直接觸發URL時得到的獲?。?http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101
查看完整描述

2 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

這個想法是,您將從用戶獲取端點以顯示視圖計數,這將使用@RequestParam完成。 根據您的要求Based on the request endPoint create the URLtoMap


(i.e methods, status, outcome, exception etc, e.g. http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101&tag=method:GET).


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

    public int count(@RequestParam(name="endPoint") final String endPoint) throws IOException, JSONException

    {

        final String URLtoMap = "http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:" + endPoint + "";

        return sendRequestToURL(URLtoMap);

    }

現在基于發送請求使用和獲取輸出使用。當我使用Spring Security時,我被重定向到登錄頁面。為了解決這個問題,我在SecurityConfig文件中添加了antMatchers,如下所示。如果您面對那么請參考這個URLtoMapHttpURLConnectionBufferedReaderJSONException: Value of type java.lang.String cannot be converted to JSONObject


public int sendRequestToURL(@PathVariable("URLtoMap") String URLtoMap) throws IOException, JSONException

{

      int count = 0;

      StringBuilder result = new StringBuilder();

      URL url = new URL(URLtoMap);

      HttpURLConnection conn = (HttpURLConnection) url.openConnection();

      conn.setRequestMethod("GET");

      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

      String line;

      while ((line = rd.readLine()) != null) {

         result.append(line);

      }

      rd.close();


      try {

            JSONObject jsonObject =new JSONObject(result.toString().replace("\"", "")); 

            JSONObject jsonCountObject = new JSONObject(jsonObject.getJSONArray("measurements").get(0).toString());

            count =(int) jsonCountObject.get("value");

        }

        catch (JSONException e) {

            e.printStackTrace();

        }


      return count;

}

安全配置


@Override

        protected void configure(HttpSecurity http) throws Exception{


             http

             .csrf().disable()

             .authorizeRequests().antMatchers("/login").permitAll()

             .antMatchers(HttpMethod.GET,"/actuator/**").permitAll() 

             .antMatchers(HttpMethod.POST,"/actuator/**").permitAll() 

}

啪.xml


<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-core</artifactId>

</dependency>


<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

</dependency>


<dependency>

  <groupId>org.json</groupId>

  <artifactId>json</artifactId>

  <version>20090211</version>

</dependency>

導入正確的軟件包


import org.json.JSONException;

import org.json.JSONObject;

import java.net.URL;

import java.net.HttpURLConnection;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;


查看完整回答
反對 回復 2022-09-01
?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

所以你想要封裝actuator/metrics/admin/count


在Java中調用Rest API的方法和庫有很多


我將添加最簡單的一個


類似的東西


public JSONObject sendRequestToURL(@PathVariable("finalURL") String urlToRead)

{

      StringBuilder result = new StringBuilder();

      URL url = new URL(urlToRead);

      HttpURLConnection conn = (HttpURLConnection) url.openConnection();

      conn.setRequestMethod("GET");

      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

      String line;

      while ((line = rd.readLine()) != null) {

         result.append(line);

      }

      rd.close();

      return new JSONObject(result.toString());  // org.json

}

編輯 1:


你快到了。只需要將字符串解析為 JSONObject。試試這個也許


String strJson = result.toString().replace("\\\"","'");

JSONObject jo = new JSONObject(strJson.substring(1,json.length()-1));

return jo;

編輯 2:


我猜你有Spring Security。


當您在內部調用API時,Spring將其視為需要身份驗證的外部調用。


作為一種解決方法,您可以從安全上下文中排除 API。/actuator


@Override

protected void configure(HttpSecurity http) throws Exception {

    http.csrf().disable().authorizeRequests()

     .antMatchers("/actuator*").permitAll()


     ...

}

或 XML 格式


<security:http  auto-config="true"  use-expressions="true"   >

    <security:intercept-url pattern="/actuator*" access="permitAll"/>


    ...

</security:http>

希望Spring安全將忽略此URL,您將不會獲得登錄表單。


查看完整回答
反對 回復 2022-09-01
  • 2 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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