0

I am getting json data from an api. The api is working well when passing it through browser or postman, but there is a null response. As json is quite complicated I stringify it. My code is below:

 String uri="api url";

 RestTemplate restTemplate1 = new RestTemplate();
 String result = restTemplate1.getForObject(uri, String.class);
 System.out.println(result);//result is null
 String jsonFormattedString = result.replaceAll("\\\\", "");//this throws null pointer exception
Alien
  • 15,141
  • 6
  • 37
  • 57
trinity
  • 159
  • 1
  • 7
  • 20
  • Please add some CURL or postman configuration which represents your http request when it runs successfully. You definitely miss some header, parameter, or smth. else. – samvel1024 Dec 12 '18 at 14:29
  • 1
    Check if your `String uri` is actually pointing to a proper endpoint. – Abdullah Khan Dec 12 '18 at 14:37
  • 1
    What is the http response code? You could change the getForObject call to the below to print out the response code: ResponseEntity response = restTemplate1.getForEntity(uri, String.class); System.out.println(response.getStatusCode()); String result = response.getBody(); – Ryan Stuetzer Dec 12 '18 at 14:55
  • First of all test your rest endpoint if you can consult it via postman or other rest client, else maybe your rest return a null ! – Abder KRIMA Dec 12 '18 at 15:10
  • You might just need to add an `Accept: application/json` header. Have a look at something like https://stackoverflow.com/questions/19238715/how-to-set-an-accept-header-on-spring-resttemplate-request/19239013 for how to set it. – pcoates Dec 12 '18 at 23:04

0 Answers0