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

4
  • What error do you get? Commented Apr 7, 2020 at 6:20
  • Also why are you using a restTemplate instead of the google java client? Commented Apr 7, 2020 at 6: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 Commented Apr 7, 2020 at 6:52
  • mvnrepository.com/artifact/com.google.maps/google-maps-services/…, from jan this year. Using the client will make this tons easier. Further info here. If you get stuck with any part of it, post another question Commented Apr 7, 2020 at 7:02

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Don't ask questions in answers.
no . I am not asking the question. I am asking about the approach what he has taken here. It is the suggestion only.
Do you know what jealous means?
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.
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;
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.