0

I have this method to make request:

    @Override
    public HttpEntity<MultiValueMap<String, String>> generateRequestEntity(Date date) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("key", "SOME_API_KEY");
        map.add("source", "SOME_API_SOURCE");
        if (date == null)
            map.add("method", "getall");
        else {
            map.add("method", "getfrom");
            map.add("date", new SimpleDateFormat("yyyy-MM-dd").format(date));
        }

        return new HttpEntity<>(map, headers);
    }

I send a request and get a response, as recommended at following link: URL

HttpEntity<MultiValueMap<String, String>> request = generateRequestEntity(date);
ResponseEntity<OnlineSell[]> response = restTemplate.postForEntity(url, request, OnlineSell[].class);
OnlineSell[] onlineSells = response.getBody();

But I have a problem. I am failing when trying to parse JSON-response. OnlineSell - class, that must keep the answer BUT I just can’t create a structure that successfully stores the values ​​of this answer. A very large and complex answer tree comes.

Answer: Can I get JSONObject from it to parse and save manually? Or can I get help with JSON parsing by previously updating this post and adding it (answer form Postman)?

Mykola
  • 118
  • 1
  • 14

1 Answers1

1

What you can do is to consider the ResponseEntity as a String. Then afterwards you can use objectMapper with readValue(),

Something like this:

ResponseEntity<String> response = restTemplate().postForEntity(url, request, String.class);

String body = response.getBody();
OnlineSell[] onlineSells = new ObjectMapper().readValue(body, OnlineSell[].class);
Léo Schneider
  • 2,085
  • 3
  • 15
  • 28
  • Thank you for help! `response.getBody()` output JSON same as i get from Postman. But on readValue-method I have this: `Cannot deserialize instance of `com.iwis.kpi.entities.OnlineSells[]` out of START_OBJECT token at (...)`. So, the problem is partially left, I can not parse JSON in POJO (entity). – Mykola Jun 17 '20 at 10:10
  • Your problem is that your JSON is not an array of OnlineSell but rather an object. If you create a class that has a list of OnlineSell then that should do the trick: `class OnlineSells {OnlineSell[] onlineSells;}` – Léo Schneider Jun 17 '20 at 10:56
  • If this solves your issue, please approve the answer ;) – Léo Schneider Jun 17 '20 at 12:25