I am learning Spring Boot and I was trying to consume an external API (Instana) but I am facing this error where I can't pull the application metrics. I can obtain the result using curl but unable to convert it in Spring Boot
@PostMapping("/metrics")
public PageLoad getPageData(){
RestTemplate restTemplate = new RestTemplate();
String customerAPIUrl = "https://apm-companyName.instana.io/api/application-monitoring/metrics/applications";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBearerAuth("tokenByInstana");
HttpEntity<String> entity = new HttpEntity <> (headers);
ResponseEntity<PageLoad> response = restTemplate.exchange(customerAPIUrl, HttpMethod.GET, entity, PageLoad.class);
return new PageLoad();
}
The error which occurs over here is
org.springframework.web.client.HttpClientErrorException$MethodNotAllowed: 405 Method Not Allowed: "{"code":405,"message":"HTTP 405 Method Not Allowed"}"
This is the response class
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PageLoad {
Object time;
List<Object> items = new ArrayList<>();
Integer page;
Integer pageSize;
Integer totalHits;
}
I think you have a few problems, to the immediate error in the question I think you need to do a GET here. So you need @GetMapping("/metrics") instead of PostMapping. This is because it looks like you are not even POSTing any data to your endpoint so the PostMapping is redundant..
The error is happening because it is expecting you to run:
curl -X POST http://localhost:8080/metrics
and I suspect you are just going curl http://localhost:8080/metrics
(which is a default GET).