Azure COSMOSDB 指标 - HttpClientErrorException $BadRequest:400 错误请求 - 无法使用 REST API
Azure COSMOSDB metrics - HttpClientErrorException$BadRequest: 400 Bad Request - Failed to consume REST API
Azure CosmosDB - "Mongo Query Request Rate" 我想要获取的指标。当我使用 postman 时,Microsoft 提供的 URL 工作正常。但是如果我使用 WebClient 或 RestTemplate,它会抛出异常:
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:79)
这是我的代码:
public Mono<DataVO> getQueryRequestRate(String subscriptionId, String resourceGroup, String cosmosDbName) {
String [] dateTimes = getDateTimes() ;
String url= "/subscriptions/"+subscriptionId+"/resourceGroups/"+resourceGroup +
"/providers/Microsoft.DocumentDb/databaseAccounts/"+cosmosDbName +
"/metrics?api-version=2015-04-08&$filter=%28name.value%20eq%20%27Mongo Query Request Rate%27%29%20and" +
"%20timeGrain%20eq%20duration%27PT5M%27%20and%20startTime%20eq%20"+dateTimes[0] +
"Z%20and%20endTime%20eq%20"+dateTimes[1]+"Z";
webClient = WebClient.builder().baseUrl("https://management.azure.com")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();
return webClient.get().uri(url)
.header("Authorization", "Bearer "+token)
.exchange()
.flatMap(resp -> resp.bodyToMono(DataVO.class));
}
public String[] getDateTimes() {
LocalDateTime ldtNow = LocalDateTime.now();
LocalDateTime ldtBefore = LocalDateTime.now().minusMinutes(5);
String[] currentDateTime = new String[2];
currentDateTime[0]=String.valueOf(ldtBefore);
currentDateTime[1]=String.valueOf(ldtNow);
return currentDateTime;
}
形成请求似乎有问题。这就是它失败的原因。
有什么帮助吗?
形成 url 时出错,我现在已更正。
public Mono<DataVO> getQueryRequestRate(String subscriptionId, String resourceGroup, String cosmosDbName) {
String [] dateTimes = getDateTimes() ;
String url= "/subscriptions/"+subscriptionId+"/resourceGroups/"+resourceGroup +
"/providers/Microsoft.DocumentDb/databaseAccounts/"+cosmosDbName +
"/metrics?api-version=2015-04-08" +
"&$filter=(name.value eq 'Mongo Query Request Rate' and "+
"startTime eq "+dateTimes[0]+"Z and endTime eq "+dateTimes[1]+"Z and timeGrain eq duration'PT5M')";
//System.out.println(url);
webClient = WebClient.builder().baseUrl("https://management.azure.com")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();
return webClient.get().uri(url)
.header("Authorization", "Bearer "+token)
.exchange()
.flatMap(resp -> resp.bodyToMono(DataVO.class));
}
Azure CosmosDB - "Mongo Query Request Rate" 我想要获取的指标。当我使用 postman 时,Microsoft 提供的 URL 工作正常。但是如果我使用 WebClient 或 RestTemplate,它会抛出异常:
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:79)
这是我的代码:
public Mono<DataVO> getQueryRequestRate(String subscriptionId, String resourceGroup, String cosmosDbName) {
String [] dateTimes = getDateTimes() ;
String url= "/subscriptions/"+subscriptionId+"/resourceGroups/"+resourceGroup +
"/providers/Microsoft.DocumentDb/databaseAccounts/"+cosmosDbName +
"/metrics?api-version=2015-04-08&$filter=%28name.value%20eq%20%27Mongo Query Request Rate%27%29%20and" +
"%20timeGrain%20eq%20duration%27PT5M%27%20and%20startTime%20eq%20"+dateTimes[0] +
"Z%20and%20endTime%20eq%20"+dateTimes[1]+"Z";
webClient = WebClient.builder().baseUrl("https://management.azure.com")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();
return webClient.get().uri(url)
.header("Authorization", "Bearer "+token)
.exchange()
.flatMap(resp -> resp.bodyToMono(DataVO.class));
}
public String[] getDateTimes() {
LocalDateTime ldtNow = LocalDateTime.now();
LocalDateTime ldtBefore = LocalDateTime.now().minusMinutes(5);
String[] currentDateTime = new String[2];
currentDateTime[0]=String.valueOf(ldtBefore);
currentDateTime[1]=String.valueOf(ldtNow);
return currentDateTime;
}
形成请求似乎有问题。这就是它失败的原因。
有什么帮助吗?
形成 url 时出错,我现在已更正。
public Mono<DataVO> getQueryRequestRate(String subscriptionId, String resourceGroup, String cosmosDbName) {
String [] dateTimes = getDateTimes() ;
String url= "/subscriptions/"+subscriptionId+"/resourceGroups/"+resourceGroup +
"/providers/Microsoft.DocumentDb/databaseAccounts/"+cosmosDbName +
"/metrics?api-version=2015-04-08" +
"&$filter=(name.value eq 'Mongo Query Request Rate' and "+
"startTime eq "+dateTimes[0]+"Z and endTime eq "+dateTimes[1]+"Z and timeGrain eq duration'PT5M')";
//System.out.println(url);
webClient = WebClient.builder().baseUrl("https://management.azure.com")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();
return webClient.get().uri(url)
.header("Authorization", "Bearer "+token)
.exchange()
.flatMap(resp -> resp.bodyToMono(DataVO.class));
}