【问题标题】:Spring RestTemplate not using complete URLSpring RestTemplate 不使用完整的 URL
【发布时间】:2020-07-19 06:41:50
【问题描述】:

我正在尝试通过 Rest Template 使用 Google Places API。除了使用 pagetoken 获取分页结果外,一切正常。页面令牌是一个很长的字符串,我尝试记录 URL 并打印它。如果我复制粘贴记录的 URL,并在浏览器中尝试,它运行良好,但 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;
    }

正常请求运行正常,但是当有页面token时,就是很长的字符串比如

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

当我从日志中复制整个 URL 并在浏览器中运行时,该 URL 运行良好

【问题讨论】:

  • 你得到什么错误?
  • 另外你为什么使用restTemplate而不是google java客户端?
  • 我收到了来自 Google API 的 INVALID_REQUEST 响应。对不起,我不熟悉 Java 客户端。找不到任何足够新的 Maven 依赖项。我再找找。任何指针表示赞赏
  • mvnrepository.com/artifact/com.google.maps/google-maps-services/…,从今年一月开始。使用客户端将使这一切变得更容易。更多信息here。如果您对其中的任何部分感到困惑,请发布另一个问题

标签: java spring rest google-maps-api-3


【解决方案1】:

为什么不使用 json 作为客户端发送格式,而在服务器端可以使用注解 @Requestbody 使用请求正文数据。

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

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

另外,您可以使用restTemplate.exchange() 方法。如何将此方法与查询参数一起使用,您可以查看this post。解释得很好。

【讨论】:

  • 不要在答案中提问。
  • 没有。我不是在问这个问题。我问的是他在这里采取的方法。这只是建议。
  • 你知道嫉妒是什么意思吗?
  • 我没有大喊大叫,尽管就问题而言,您的回答毫无意义,最好作为评论。令人惊讶的是它有多个赞成票。
【解决方案2】:

试试这个:

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

【讨论】:

    【解决方案3】:

    问题是我请求 nextPage 太快了。与 Spring 或 Rest 模板无关。在这里找到答案

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

    【讨论】:

      猜你喜欢
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 2011-12-18
      相关资源
      最近更新 更多