Found a similar issue while calling Prometheus API from Java. Followed @Sotirios Delimanolis. It is working now. Below is the sample code to get CPU metric from Prometheus.
String mode = "idle";
String sort = "{mode=\""+mode+"\"}";
String metric = "node_cpu_seconds_total{sort}";
String rate = "rate(" + metric + "[5m])";
String avg = "avg by (instance) (" + rate + ")";
String calculation = "100 - (" + avg + " * 100)";
System.out.println("calc : " + calculation);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:9090/api/v1/query_range")
.queryParam("query", calculation)
.queryParam("start", 1740048845)
.queryParam("end", 1740048905)
.queryParam("step", 15);
String prometheusUrl = builder.build().toString();
Map<String, Object> response = restTemplate.getForObject(query, Map.class, sort);