Spring RestTemplate not using complete URL

Viewed 774

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

3 Answers

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.

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;
}
Related