2

I've an api, which read from an OracleDB. If i call it directly it returns:

{
 ...
 ...
 numericField: 910010000346422907
 ...
 otherNumericField: 2018020800
 ...
}

but if i call it through a springboot java server using

URI targetUrl= UriComponentsBuilder.fromUriString(url)
            .queryParams(queryParameters)
            .build()
            .encode()
            .toUri();

RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(targetUrl, String.class);

values in result string looks like

numericFiled: 9.1001000034642291E17 .... otherNumericField: 2.0180208E9

The api returns Content-Type application/json; charset=UTF-8. How could I correctly read numeric value correctly?

  • Why you are converting Object to string? It must be something like restTemplate.getForObject(targetUrl, YourObj.class); – Naveed Kamran Mar 06 '19 at 16:47
  • I would like to parse json without know how it's made by before, i don't have any YourObj.class –  Mar 06 '19 at 16:48

1 Answers1

1

You have to create a POJO class with all the properties, something like this, you can use lombok to create Getters and Setters for you by annotations lombok

public class ResponseData {

      private double numericFiled

      private double otherNumericField

   // Getters and Setters

  }

And in the getForObject call just pass the Class type

ResponseData result = restTemplate.getForObject(targetUrl, ResponseData.class);

If you don't know the JSON schema then use Map<Object,Object> map, but always recommended parsing to model and you can also ignore the fields that you don't need and don't know

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98