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,您将不会获得登录表单。
添加回答
举报