I have a requirement to count the views on each endpoint. The idea is to create one common Request Count Mapping for all endpoints which should return the view count based on a
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
...
And hopefully Spring security will ignore this URL and you will not get Login Form.