2

I'm trying to use Google Places API using Rest Template. Everything works fine except for using the pagetoken for getting paginated results. The page token is an very long string, and I tried logging the URL and printing it. If I copy-paste the logged URL, and try it in a browser, it runs fine, but the rest template request is being identified as invalid by the API.

@ResponseBody
    @GetMapping("/nearby")
    public String nearbyController(@RequestParam String keyword, @RequestParam String location, @RequestParam String type, @RequestParam String radius, @RequestParam(defaultValue = "") String pagetoken) throws RestClientException, URISyntaxException {
        final String uri = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=Key_Here&keyword=" + keyword + "&type=" + type + "&radius=" + radius + "&location=" + location + "&pagetoken=" + pagetoken;
        RestTemplate restTemplate = new RestTemplate();
        logger.info(uri);
        String result = restTemplate.getForObject(new URI(uri), String.class);
        return result;
    }

The normal requests run fine, but when there is a page token, which is a very long string such as

"CsQCQAEAALL-mDkGLJnijEldNf7CbsrkWX_a2SizcU-i60AkJrb20EFAnNMb8Pgm4wYrRQ1bXMOEm1dYbxxojJm14p43cDVylw_6X6RU-5p7hoLI5N3LJ_DMERR_Wwc_n08EeIf4xLk1ZJUJtmEVuAHvDHBf68VALb7RBXvurykkfN4Gb6fUFCQ0xmIhSAGaW9BAtB08Z6EsYdk8HhiRzgswUE4XuA6LBaQguldJXmo5SxJjqC8x5HCfeL3ZzG_DNAbhrx8ozlfDPUYLQk415mO1pw2SJeCAbfogrgaNvqPO1LnhuCzOW6wphB_y9401QwUhtVqwen0-yCJgOHju9Ow0ihJM9ht6k3PjMKDzxkUey4i7Xw8L9dP9zv3IquA3lzaOOgCdqkZ5U37XohJ78PbUWTh55-1eUf1sH04GHs1RWTbzoJbwEhB06aFckoVAbM7Oiz1zAj2YGhT0JEcQ02V7RuH95-a-dcHFew5a3A"

The URL runs fine when I copy past the entire URL from the log and run it in a browser

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
gigin26
  • 533
  • 1
  • 5
  • 16
  • What error do you get? – 123 Apr 07 '20 at 06:20
  • Also why are you using a restTemplate instead of the google java client? – 123 Apr 07 '20 at 06:25
  • I'm getting an INVALID_REQUEST response from Google API. I'm sorry I'm not familiar with the Java Client. Couldn't find any Maven dependencies that are recent enough. I'll search again. Any pointers are appreciated – gigin26 Apr 07 '20 at 06:52
  • https://mvnrepository.com/artifact/com.google.maps/google-maps-services/0.11.0, from jan this year. Using the client will make this tons easier. Further info [here](https://github.com/googlemaps/google-maps-services-java). If you get stuck with any part of it, post another question – 123 Apr 07 '20 at 07:02

3 Answers3

2

Why are you not using json as a client sending format and in server side you can consume the request body data using the annotation @Requestbody.

In your application you are trying to send data to server side using query param which does not allow you send the string which is long in length.

please try to send the data in json format to server side from client side.It is one of the standard way to communicate with client and server in rest application.

And Alternative you can use restTemplate.exchange() method. how to use this method with query param you can check this post. It is well explained.

Desi boys
  • 29
  • 3
  • Don't ask questions in answers. – 123 Apr 07 '20 at 06:26
  • no . I am not asking the question. I am asking about the approach what he has taken here. It is the suggestion only. – Desi boys Apr 07 '20 at 06:28
  • Do you know what jealous means? – 123 Apr 07 '20 at 07:06
  • I'm not shouting, your answer makes no sense though with respect to the question and would be better served as a comment. It's surprising that it has multiple upvotes. – 123 Apr 07 '20 at 07:11
0

Try this:

@ResponseBody
@GetMapping("/nearby")
public String nearbyController(@RequestParam String keyword, @RequestParam String location, @RequestParam String type, @RequestParam String radius, @RequestParam(defaultValue = "") String pagetoken) throws RestClientException, URISyntaxException {
    final String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json";
    Map<String, String> params = new HashMap<>();
    params.put("key", "Key_Here");
    params.put("keyword", keyword);
    params.put("type", type);
    params.put("radius", radius);
    params.put("location", location);
    params.put("pagetoken", pagetoken);

    HttpEntity httpEntity = new HttpEntity(headers);
    RestTemplate restTemplate = new RestTemplate();

    logger.info(url);
    String result = restTemplate.getForObject(url, String.class, httpEntity);
    return result;
}
0

The issue was that I was requesting the nextPage too soon. Nothing to do with Spring or Rest Template. Found the Answer here

https://stackoverflow.com/a/21266061/4514541

gigin26
  • 533
  • 1
  • 5
  • 16