我使用标准的 Spring Boot webclient 将 http POST 请求发送到 azure timeseries 洞察。
在响应正文中,我错过了值。
环境:
- 视窗 10
- Java 11(ibm-semeru_jdk-11.0.13+8_openj9 和 amazon-corretto_jdk11.0.13_8)
- 春季启动 2.5.6
- OkHttpClient 4.9.2
- IntelliJ IDEA 2021.2.3
这是我的步骤:
我用spring boot webclient试试这个
final ResponseEntity<String> responseEntity = webClient.post()
.uri(tsiUrl)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(requestBody)
.retrieve()
.toEntity(String.class)
.block();
return responseEntity != null ? responseEntity.getBody() : null;
这与 OkHttpClient (验证响应,但我得到相同的响应内容)
public class App
{
public static final okhttp3.MediaType JSON = okhttp3.MediaType.get("application/json; charset=utf-8");
public static void main( String[] args )
{
String requestBody = "{\n"
+ " \"aggregateSeries\": {\n"
+ " \"searchSpan\": {\n"
+ " \"from\": \"2021-01-01T00:00Z\",\n"
+ " \"to\": \"2021-12-31T00:00:01Z\"\n"
+ " },\n"
+ " \"timeSeriesId\": [\n"
+ " \"edge-goldwind-qa-002-astraios\",\n"
+ " \"GcmProcessed\"\n"
+ " ],\n"
+ " \"interval\": \"P1D\",\n"
+ " \"inlineVariables\": {\n"
+ " \"gcm01DeteriorationMax\": {\n"
+ " \"kind\": \"numeric\",\n"
+ " \"value\": {\n"
+ " \"tsx\": \"$event.GCM01Deterioration.Double\"\n"
+ " },\n"
+ " \"filter\": null,\n"
+ " \"aggregation\": {\n"
+ " \"tsx\": \"max($value)\"\n"
+ " }\n"
+ " },\"gcm01TemperatureOpticsMax\": {\n"
+ " \"kind\": \"numeric\",\n"
+ " \"value\": {\n"
+ " \"tsx\": \"$event.GCM01TemperatureOptics.Long\"\n"
+ " },\n"
+ " \"filter\": null,\n"
+ " \"aggregation\": {\n"
+ " \"tsx\": \"max($value)\"\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " \"projectedVariables\": [\n"
+ " \"gcm01DeteriorationMax\",\n"
+ " \"gcm01TemperatureOpticsMax\"\n"
+ " ]\n"
+ " }\n"
+ "}";
String token = "bearertoken"; //removed original bearer token
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.header("Authorization", "Bearer " + token)
.url("https://1ff924d7-55b5-48c7-8c29-7fcbc18b8776.env.timeseries.azure.cn/timeseries/query?api-version=2020-07-31&storeType=WarmStore")
.post(RequestBody.create(requestBody, JSON))
.build();
Response response = client.newCall(request).execute();
final ResponseBody body = response.body();
final String string = body.string();
} catch (Exception e) {
e.fillInStackTrace();
}
}
}
我发送这个 POST 正文:
{
"aggregateSeries": {
"searchSpan": {
"from": "2021-01-01T00:00Z",
"to": "2021-12-31T00:00:01Z"
},
"timeSeriesId": [
"edge-goldwind-qa-002-astraios",
"GcmProcessed"
],
"interval": "P1D",
"inlineVariables": {
"gcm01DeteriorationMax": {
"kind": "numeric",
"value": {
"tsx": "$event.GCM01Deterioration.Double"
},
"filter": null,
"aggregation": {
"tsx": "max($value)"
}
},"gcm01TemperatureOpticsMax": {
"kind": "numeric",
"value": {
"tsx": "$event.GCM01TemperatureOptics.Long"
},
"filter": null,
"aggregation": {
"tsx": "max($value)"
}
}
},
"projectedVariables": [
"gcm01DeteriorationMax",
"gcm01TemperatureOpticsMax"
]
}
}
Spring Boot webclient和OkHttpClient的结果(不是预期的)
{"values":[null,..,null,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,null,...,null],"name":"gcm01DeteriorationMax","type":"Double"}
(我删除了所有空值,以简单地查看差异)
但是,如果我使用Postman发送相同的 POST,我会得到这个结果(预期):
{"values":[null,..,null,69.209999084472656,95.569999694824219,87.209999084472656,90.419998168945313,89.419998168945313,65.120002746582031,73.19000244140625,75.6500015258789,77.44000244140625,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,null,null,null,null,null,null,null,null,null,100.0,100.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,null,..,null],"name":"gcm01DeteriorationMax","type":"Double"}
如您所见,邮递员的结果包含更多的值和更少的空值。我已经尝试使用 .Net Core 5 httpclient 进行相同的 POST,并且得到与使用 Postman 相同的结果。
我的问题是,有人知道这里出了什么问题吗?