Spring RestTemplate 未使用完整 URL
Spring RestTemplate not using complete URL
我正在尝试使用 Google Places API 通过 Rest 模板。除了使用 pagetoken 获取分页结果外,一切正常。页面标记是一个很长的字符串,我尝试记录 URL 并打印它。如果我复制粘贴记录的 URL,并在浏览器中尝试,它 运行 没问题,但其余模板请求被 API.[=14 识别为无效=]
@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;
}
正常请求运行还好,但是当有page token的时候,就是一个很长的字符串,比如
"CsQCQAEAALL-mDkGLJnijEldNf7CbsrkWX_a2SizcU-i60AkJrb20EFAnNMb8Pgm4wYrRQ1bXMOEm1dYbxxojJm14p43cDVylw_6X6RU-5p7hoLI5N3LJ_DMERR_Wwc_n08EeIf4xLk1ZJUJtmEVuAHvDHBf68VALb7RBXvurykkfN4Gb6fUFCQ0xmIhSAGaW9BAtB08Z6EsYdk8HhiRzgswUE4XuA6LBaQguldJXmo5SxJjqC8x5HCfeL3ZzG_DNAbhrx8ozlfDPUYLQk415mO1pw2SJeCAbfogrgaNvqPO1LnhuCzOW6wphB_y9401QwUhtVqwen0-yCJgOHju9Ow0ihJM9ht6k3PjMKDzxkUey4i7Xw8L9dP9zv3IquA3lzaOOgCdqkZ5U37XohJ78PbUWTh55-1eUf1sH04GHs1RWTbzoJbwEhB06aFckoVAbM7Oiz1zAj2YGhT0JEcQ02V7RuH95-a-dcHFew5a3A"
当我从日志中复制整个 URL 并在浏览器中 运行 时,URL 运行 没问题
为什么不使用 json 作为客户端发送格式,而在服务器端您可以使用注释 @Requestbody
使用请求正文数据。
在您的应用程序中,您正尝试使用查询参数将数据发送到服务器端,该参数不允许您发送长度过长的字符串。
请尝试从客户端发送 json 格式的数据到服务器端 side.It 是在 rest 应用程序中与客户端和服务器通信的标准方式之一。
您可以使用 restTemplate.exchange()
方法替代。如何将此方法与查询参数一起使用,您可以查看 this post。解释的很好。
试试这个:
@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;
}
问题是我过早地请求下一页。与 Spring 或 Rest 模板无关。在这里找到答案
我正在尝试使用 Google Places API 通过 Rest 模板。除了使用 pagetoken 获取分页结果外,一切正常。页面标记是一个很长的字符串,我尝试记录 URL 并打印它。如果我复制粘贴记录的 URL,并在浏览器中尝试,它 运行 没问题,但其余模板请求被 API.[=14 识别为无效=]
@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;
}
正常请求运行还好,但是当有page token的时候,就是一个很长的字符串,比如
"CsQCQAEAALL-mDkGLJnijEldNf7CbsrkWX_a2SizcU-i60AkJrb20EFAnNMb8Pgm4wYrRQ1bXMOEm1dYbxxojJm14p43cDVylw_6X6RU-5p7hoLI5N3LJ_DMERR_Wwc_n08EeIf4xLk1ZJUJtmEVuAHvDHBf68VALb7RBXvurykkfN4Gb6fUFCQ0xmIhSAGaW9BAtB08Z6EsYdk8HhiRzgswUE4XuA6LBaQguldJXmo5SxJjqC8x5HCfeL3ZzG_DNAbhrx8ozlfDPUYLQk415mO1pw2SJeCAbfogrgaNvqPO1LnhuCzOW6wphB_y9401QwUhtVqwen0-yCJgOHju9Ow0ihJM9ht6k3PjMKDzxkUey4i7Xw8L9dP9zv3IquA3lzaOOgCdqkZ5U37XohJ78PbUWTh55-1eUf1sH04GHs1RWTbzoJbwEhB06aFckoVAbM7Oiz1zAj2YGhT0JEcQ02V7RuH95-a-dcHFew5a3A"
当我从日志中复制整个 URL 并在浏览器中 运行 时,URL 运行 没问题
为什么不使用 json 作为客户端发送格式,而在服务器端您可以使用注释 @Requestbody
使用请求正文数据。
在您的应用程序中,您正尝试使用查询参数将数据发送到服务器端,该参数不允许您发送长度过长的字符串。
请尝试从客户端发送 json 格式的数据到服务器端 side.It 是在 rest 应用程序中与客户端和服务器通信的标准方式之一。
您可以使用 restTemplate.exchange()
方法替代。如何将此方法与查询参数一起使用,您可以查看 this post。解释的很好。
试试这个:
@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;
}
问题是我过早地请求下一页。与 Spring 或 Rest 模板无关。在这里找到答案