Spring RestTemplate未使用完整URL。

huangapple go评论76阅读模式
英文:

Spring RestTemplate not using complete URL

问题

我正在尝试使用 Rest Template 调用 Google Places API。除了使用页面令牌获取分页结果之外,一切工作正常。页面令牌是一个非常长的字符串,我尝试记录URL并将其打印出来。如果我复制粘贴记录的URL,并在浏览器中尝试运行它,它可以正常运行,但是 API 将 Rest Template 请求识别为无效。

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

普通请求正常运行,但当存在一个如下所示的非常长的页面令牌时:

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

从日志中复制整个URL并在浏览器中运行时,URL可以正常运行。

英文:

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

答案1

得分: 2

为什么你不使用JSON作为客户端发送格式,在服务器端你可以使用@RequestBody注解来消费请求体数据。

在你的应用程序中,你试图使用查询参数将数据发送到服务器端,但查询参数不允许你发送长度较长的字符串。

请尝试使用JSON格式将数据从客户端发送到服务器端。这是在REST应用程序中与客户端和服务器通信的标准方法之一。

另外,你还可以使用restTemplate.exchange()方法。关于如何在查询参数中使用此方法,你可以查看这篇文章,其中有很好的解释。

英文:

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.

答案2

得分: 0

请尝试以下代码:

@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;
}
英文:

Try this:

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

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

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

答案3

得分: 0

问题是我请求下一页太早了,与Spring或Rest Template无关。在这里找到答案:

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

英文:

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

huangapple
  • 本文由 发表于 2020年4月7日 14:13:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61073824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定