So you want to encapsulate actuator/metrics
with /admin/count
There are many ways and library for calling Rest API in Java
I will add the simplest one
Something like this
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
}
Edit 1:
You are almost there. Just need to parse String to JSONObject. Try this maybe
String strJson = result.toString().replace(""","'");
JSONObject jo = new JSONObject(strJson.substring(1,json.length()-1));
return jo;
Edit 2:
I guess you have Spring Security in place.
And when you are calling an API internally, Spring is treating as an external call which requires Authentication.
As a workaround, you can exclude /actuator
API from security context.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers("/actuator*").permitAll()
...
}
or in XML
<security:http auto-config="true" use-expressions="true" >
<security:intercept-url pattern="/actuator*" access="permitAll"/>
...
</security:http>
And hopefully Spring security will ignore this URL and you will not get Login Form.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…