Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring RestTemplate未使用完整URL_Java_Spring_Rest_Google Maps Api 3 - Fatal编程技术网

Java Spring RestTemplate未使用完整URL

Java Spring RestTemplate未使用完整URL,java,spring,rest,google-maps-api-3,Java,Spring,Rest,Google Maps Api 3,我正在尝试使用Rest模板使用GooglePlacesAPI。除了使用pagetoken获得分页结果外,其他一切都可以正常工作。页面标记是一个很长的字符串,我尝试记录URL并打印它。如果我复制粘贴记录的URL,并在浏览器中尝试,它运行正常,但API将rest模板请求标识为无效 @ResponseBody @GetMapping("/nearby") public String nearbyController(@RequestParam String keyword, @Requ

我正在尝试使用Rest模板使用GooglePlacesAPI。除了使用pagetoken获得分页结果外,其他一切都可以正常工作。页面标记是一个很长的字符串,我尝试记录URL并打印它。如果我复制粘贴记录的URL,并在浏览器中尝试,它运行正常,但API将rest模板请求标识为无效

@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;
    }
正常的请求运行正常,但是当有一个页面标记时,它是一个非常长的字符串,例如

"2)2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2(2)2)2(2)2)2(2)2(2)2)2(2)2)2(2)2)2(2)2)2(2)2)2)2(2)2)2)2(2)2)2)2)2(2)2)2)2(2)2)2(2)2)2)2(2)2)2)2(2)2)2)2)2(2)2)2)2)2)2(2)2)2)2)2)2(2)2)2)2)2)2(2)2)2)2)2)2)2(2)2)2)2)2)2)2)2 WHTVQWEN0-yCJgOHju9Ow0ihJM9ht6k3PjMKDzxkUey4i7Xw8L9dP9zv3IquA3lzaOOgCdqkZ5U37XohJ78PbUWTh55-1eUf1sH04GHs1RWTbzoJbwEhB06aFckoVAbM7Oiz1zAj2YGhT0JEcQ02V7RuH95-a-dcHFew5a3A“


当我从日志中复制整个URL并在浏览器中运行时,URL运行良好。为什么不使用json作为客户端发送格式,而在服务器端,可以使用注释
@Requestbody
使用请求正文数据

在应用程序中,您试图使用query param将数据发送到服务器端,但query param不允许您发送长度较长的字符串

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

或者,您可以使用
restemplate.exchange()
方法。如何将此方法与查询参数一起使用,您可以检查。这一点解释得很清楚。

尝试以下方法:

@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;
}
@ResponseBody
@GetMapping(“/附近”)
公共字符串nearbyController(@RequestParam String关键字,@RequestParam String位置,@RequestParam String类型,@RequestParam String半径,@RequestParam(defaultValue=”“)String pagetoken)抛出RestClientException、URISyntaxException{
最终字符串url=”https://maps.googleapis.com/maps/api/place/nearbysearch/json";
Map params=新的HashMap();
参数put(“key”,“key_在此”);
参数put(“关键字”,关键字);
参数put(“类型”,类型);
参数put(“半径”,半径);
参数put(“位置”,位置);
参数put(“pagetoken”,pagetoken);
HttpEntity HttpEntity=新的HttpEntity(标题);
RestTemplate RestTemplate=新RestTemplate();
logger.info(url);
字符串结果=restTemplate.getForObject(url、String.class、httpEntity);
返回结果;
}

问题是我请求下一页的时间太短。与Spring或Rest模板无关。在此处找到答案


您会遇到什么错误?为什么要使用restTemplate而不是google java客户端?我从google API收到一个无效的请求响应。很抱歉,我不熟悉java客户端。找不到任何足够新的Maven依赖项。我将再次搜索。从今年1月开始,欢迎提供任何提示。使用cl客户会让这更容易。进一步的信息。如果你被困在其中的任何部分,发布另一个问题。不要在回答中提问。不。我不是在问问题。我是在问他在这里采取的方法。这只是建议。你知道嫉妒意味着什么吗?我不是在喊,你的回答毫无意义,尽管带着尊重对于这个问题,最好作为一个评论。令人惊讶的是,它有多张赞成票。