Response of an Http request not contain the whole data - java

I send a http POST request to an azure timeseries insights by using the standard Spring Boot weblient.
Inside the response body I miss values.
Environment:
Windows 10
Java 11 (ibm-semeru_jdk-11.0.13+8_openj9 and amazon-corretto_jdk11.0.13_8)
Spring Boot 2.5.6
OkHttpClient 4.9.2
IntelliJ IDEA 2021.2.3
Here are my steps:
I try this with the 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;
and this with OkHttpClient (to verify the response, but I get the same reponse content)
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();
}
}
}
and I send this POST body:
{
"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"
]
}
}
The result of the Spring Boot webclient and OkHttpClient (not expected)
{"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"}
(I removed all the null values, to see the differences simple)
But if I send the same POST with Postman I get this result (expected):
{"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"}
As you can see the result of the Postman contains more values and less null values.
I have tried the same POST with .Net Core 5 httpclient and I get the same results as with the Postman.
My question is, does anyone have an idea what is going wrong here?

Related

Deeply nested JSON response from third party

I'm getting this deeply nested JSON response from an api that I have no control,
What should be the best way to get to "generalDetails" and then find the first true value under security, address, account and mobile?
{
"info_code": "201",
"info_description": "info description",
"data": {
"status": "here goes the status",
"failure_data": {
"source": "anySource",
"details": {
"data": {
"server_response": {
"generalDetails": {
"security": {
"isAccountLocked": "false"
},
"address": {
"isAddresExists": "true"
},
"account": {
"accountExists": "true",
"isValidAccount": "true"
},
"mobile": {
"mobileExists": "true"
}
}
}
}
}
}
}
}
My request looks like:
#Autowired
private WebClient.Builder webClientBuilder;
String resp = webClientBuilder.build().get().uri(URL)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class).block();
First, build the model, automatic here https://codebeautify.org/json-to-java-converter.
Then read data with the model
.bodyToMono(MyData.class)
Then decide how you want evaluate the requirement find the first true value under security, address, account and mobile.
What means "first" ? JSON has no natural order without indicating explicity (e.g. field "order": 2).
N.B. "true", "false" of the response are Strings, not booleans.
Once you have the model with data, you may do:
Object firstTrue(GeneralDetails gd) {
// No null checks here
if ("true".equals(gd.getSecurtity().isLockedAccount())) return gd.getSecurtity();
if ("true".equals(gd.getAddress().isAddressExists())) return gd.getAddress();
if ("true".equals(gd.getAccount().isAccountExists()) || "true".equals(gd.getAccount().isAccountValid())) return gd.getAccount();
if ("true".equals(gd.getMobile().isMobileExists())) return gd.getMobile();
return null;
}
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"{" +
" \"info_code\": \"201\"," +
" \"info_description\": \"info description\"," +
" \"data\": {" +
" \"status\": \"here goes the status\"," +
" \"failure_data\": {" +
" \"source\": \"anySource\"," +
" \"details\": {" +
" \"data\": {" +
" \"server_response\": {" +
" \"generalDetails\": {" +
" \"security\": {" +
" \"isAccountLocked\": \"false\"" +
" }," +
" \"address\": {" +
" \"isAddresExists\": \"true\"" +
" }," +
" \"account\": {" +
" \"accountExists\": \"true\"," +
" \"isValidAccount\": \"true\"" +
" }," +
" \"mobile\": {" +
" \"mobileExists\": \"true\"" +
" }" +
" }" +
" }" +
" }" +
" }" +
" }" +
" }" +
"}");
Query
JsonNode node = josson.getNode(
"data.failure_data.details.data.server_response" +
".generalDetails.**.mergeObjects().assort().[*]");
System.out.println(node.toPrettyString());
Output
{
"isAddresExists" : "true"
}
If changed isAddresExists and accountExists to false
" \"generalDetails\": {" +
" \"security\": {" +
" \"isAccountLocked\": \"false\"" +
" }," +
" \"address\": {" +
" \"isAddresExists\": \"false\"" +
" }," +
" \"account\": {" +
" \"accountExists\": \"false\"," +
" \"isValidAccount\": \"true\"" +
" }," +
" \"mobile\": {" +
" \"mobileExists\": \"true\"" +
" }" +
" }" +
Output
{
"isValidAccount" : "true"
}
If you only want the key name
String firstTureKey = josson.getString(
"data.failure_data.details.data.server_response" +
".generalDetails.**.mergeObjects().assort().[*].keys().[0]");
System.out.println(firstTureKey);
Output
isValidAccount

Multiple JSONs in one Request --> Each of them to an Object

I am requesting via OKHttpClient data from my api, and im trying with an getAll() to split up the JSON response into my objects i need.
Here is an example for my response i get:
[
{
"value":"data",
"id":5
},
{
"value":"data",
"id":6
},
{
"value":"data",
"id":7
},
{
"value":"data",
"id":8
},
{
"value":"data",
"id":9
},
{
"value":"value",
"id":10
}
]
I believe you can use GSON in Android, so kindly see the code below:
static class SimpleExample{
private String value;
private Integer id;
}
public static void main(String[] args){
String json = "[{\n"
+ " \"value\":\"data\",\n"
+ " \"id\":5\n"
+ " },\n"
+ " {\n"
+ " \"value\":\"data\",\n"
+ " \"id\":6\n"
+ " },\n"
+ " {\n"
+ " \"value\":\"data\",\n"
+ " \"id\":7\n"
+ " },\n"
+ " {\n"
+ " \"value\":\"data\",\n"
+ " \"id\":8\n"
+ " },\n"
+ " {\n"
+ " \"value\":\"data\",\n"
+ " \"id\":9\n"
+ " },\n"
+ " {\n"
+ " \"value\":\"value\",\n"
+ " \"id\":10\n"
+ " }\n"
+ "]";
final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();
SimpleExample[] allObjects = gson.fromJson(json, SimpleExample[].class);
System.out.println(allObjects.length);
}
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>

getting Webhook response error (206) when trying to give response Dynamically using webhook On Dialogflow

I am trying to make a google actions using DialogFlow api in java.
I am using Webhook for request Response for actions on DialogFlow Shown in
below image.
when Trying This Code it works fine and gives proper response as dialogflow have predefined actions facility.
Code:
#PostMapping("/webhook")
public ResponseEntity payload(RequestBody FulfillmentResponse fulfillmentResponse) {
log.info(fulfillmentResponse.getQueryResult().getQueryText());
return ResponseEntity.ok(HttpStatus.OK);
}
But When I am giving response Dynamically. It gives me an error.
Code:
#PostMapping("/webhook")
public ResponseEntity payload(RequestBody FulfillmentResponse fulfillmentResponse) {
log.info(fulfillmentResponse.getQueryResult().getQueryText());
return ResponseEntity.ok("{\n" +
" \"data\": {\n" +
" \"google\": {\n" +
" \"expectUserResponse\": true,\n" +
" \"richResponse\": {\n" +
" \"items\": [\n" +
" {\n" +
" \"simpleResponse\": {\n" +
" \"textToSpeech\": \"Choose a item\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"systemIntent\": {\n" +
" \"intent\": \"assistant.intent.action.TEXT\",\n" +
" \"data\": {\n" +
" \"#type\": \"type.googleapis.com/google.actions.v2.OptionValueSpec\",\n" +
" \"listSelect\": {\n" +
" \"title\": \"Hello\",\n" +
" \"items\": [\n" +
" {\n" +
" \"optionInfo\": {\n" +
" \"key\": \"first title\"\n" +
" },\n" +
" \"description\": \"first description\",\n" +
" \"image\": {\n" +
" \"url\": \"https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png\",\n" +
" \"accessibilityText\": \"first alt\"\n" +
" },\n" +
" \"title\": \"first title\"\n" +
" },\n" +
" {\n" +
" \"optionInfo\": {\n" +
" \"key\": \"second\"\n" +
" },\n" +
" \"description\": \"second description\",\n" +
" \"image\": {\n" +
" \"url\": \"https://lh3.googleusercontent.com/Nu3a6F80WfixUqf_ec_vgXy_c0-0r4VLJRXjVFF_X_CIilEu8B9fT35qyTEj_PEsKw\",\n" +
" \"accessibilityText\": \"second alt\"\n" +
" },\n" +
" \"title\": \"second title\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}");
}
Error:
2018-11-02 16:14:43.906 IST Error in fulfillment status received from app endpoint. See ResponseMetadata in the response. Status code: 14. Error message: Webhook error (206)
{
insertId: "6nwj8wf153t5q"
labels: {
channel: "preview"
querystream: "GOOGLE_USER"
source: "AOG_REQUEST_RESPONSE"
}
logName: "projects/elysiot-217606/logs/actions.googleapis.com%2Factions"
receiveTimestamp: "2018-11-02T10:44:43.940057016Z"
resource: {
labels: {
action_id: "actions.intent.TEXT"
project_id: "elysiot-217606"
version_id: ""
}
type: "assistant_action"
}
severity: "ERROR"
textPayload: "Error in fulfillment status received from app endpoint. See ResponseMetadata in the response. Status code: 14. Error message: Webhook error (206)"
timestamp: "2018-11-02T10:44:43.906927701Z"
trace: "projects/847724381623/traces/ABwppHFGjhCqYgY_YpSxJp5p9-s6NpvBRVzWdzGRhfypm0eZcqzYjDqjCVsdpxVXofc4xpOFLs4eAtWf9Ek"
}
same error in the form of screenshot:
I assume you are building the JSON response using Java. The request is sent from AoG to Dialogflow which invokes your webhook. In this case, Dialogflow wraps the original AoG request into a "originalDetectIntentRequest" as described in https://developers.google.com/actions/build/json/dialogflow-webhook-json
Since you are parsing the JSON request and building the response in your webhook, you should refer to the above URL for the raw JSON protocol.
Hope this helps.

elasticsearch index mapping

I need to create and update index mapping using jest and transport client , but when i try with this tutorial https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-admin-indices.html#java-admin-indices-create-index-settings
i have this problem:
Multiple markers at this line
- Syntax error on token ")", delete this token
- Syntax error on token ".", # expected after
This is my code:
Client client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
IndicesAdminClient indicesAdminClient = client.admin().indices();
indicesAdminClient.prepareCreate("calls")
.setSettings(Settings.builder()
.put("index.number_of_shards", 10)
)
.addMapping("call", "{\n" +
" \"properties\": {\n" +
" \"id\": {\n" +
" \"type\": \"string\"},\n" +
" \"number\": {\n" +
" \"type\": \"string\"},\n" +
" \"name\": {\n" +
" \"type\": \"string\"}\n" +
" }\n" +
" }")
.get();
String json = "{" +
"\"id\":\"1\"," +
"\"number\":\"123333333\"," +
"\"name\":\"Sharon Tries Elastic\"" +
"}";
IndexResponse response = client.prepareIndex("calls", "call")
.setSource(json)
.get();
// Index name
String _index = response.getIndex();
// Type name
String _type = response.getType();
// Document ID (generated or not)
String _id = response.getId();
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
}
can help please
thank you

Elasticsearch: rewrite a query using java native api

I have this query in Elasticsearch that is working perfectly if I run it from the command line:
POST http://localhost:9200/YOUR_INDEX_NAME/_search/
{
"size": 0,
"aggs": {
"autocomplete": {
"terms": {
"field": "autocomplete",
"order": {
"_count": "desc"
},
"include": {
"pattern": "c.*"
}
}
}
},
"query": {
"prefix": {
"autocomplete": {
"value": "c"
}
}
}
}
I have tried to rewrite it in java using the native client:
SearchResponse searchResponse2 = newClient.prepareSearch(INDEX_NAME)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery("{\n" +
" \"size\": 0,\n" +
" \"aggs\": {\n" +
" \"autocomplete\": {\n" +
" \"terms\": {\n" +
" \"field\": \"autocomplete\",\n" +
" \"order\": {\n" +
" \"_count\": \"desc\"\n" +
" },\n" +
" \"include\": {\n" +
" \"pattern\": \"c.*\"\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"query\": {\n" +
" \"prefix\": {\n" +
" \"autocomplete\": {\n" +
" \"value\": \"c\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}").get();
for (SearchHit res : searchResponse2.getHits()){
System.out.println(res.getSourceAsString());
}
Seems, that I'm missing something in this translation process. Thanks in advance
The Java client setQuery() method doesn't take a String with the JSON query, you need to build the query using the QueryBuilders helper methods and build the aggregation your the AggregationBuilders helper methods.
In your case that would go like this:
// build the aggregation
TermsBuilder agg = AggregationBuilders.terms("autocomplete")
.field("autocomplete")
.include("c.*")
.order(Terms.Order.count(false));
// build the query
SearchResponse searchResponse2 = newClient.prepareSearch(INDEX_NAME)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.prefixQuery("autocomplete", "c"))
.addAggregation(agg)
.get();

Categories