2 回答

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;

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,您將不會獲得登錄表單。
添加回答
舉報