简体   繁体   English

Google 地图上的附近搜索在 Springboot 应用程序中返回 INVALID_REQUEST

[英]Nearby search on Google Maps returns INVALID_REQUEST in Springboot application

I've tried to build up an application based on Springboot, in which there is request to query places based on Google map API. The final URL is print as following:我试图建立一个基于Springboot的应用程序,其中有请求查询基于Google map API的地方。最终URL打印如下:

2023-04-21 14:23:53.417 INFO  [pool-2-thread-1] c.h.s.c.s.GoogleBackendSearcher.nearbySearch - search url: https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=MY_API_KEY&keyword=food&location=48.1317547%2C11.5694659&language=US&radius=1500

It seems OK since the same URL put into Postman and the explore, there are valid results returned.似乎没问题,因为将相同的 URL 放入 Postman 并进行探索,返回了有效结果。 But the wired thing is that there is always response as INVALID_REQUEST when running the Springboot application.但是有线的事情是在运行 Springboot 应用程序时总是有 INVALID_REQUEST 响应。 Can anyone help to share suggestions about what's the problem.任何人都可以帮助分享有关问题所在的建议。

The class to build up the search URL: class建立搜索URL:

public class GoogleBackendSearcher {
protected String autoSuggestAPI() {
    return "https://maps.googleapis.com/maps/api/place/nearbysearch/json";
}
protected String apiKey(){
    return MY_API_KEY;
}

private boolean nearbySearch(SearchRequest request) {
    String url = UriComponentsBuilder.fromUriString(autoSuggestAPI())
            .queryParam("keyword", request.getQuery())
            .queryParam("location", UriUtils.encode(String.valueOf(request.getLocation()), StandardCharsets.UTF_8))
            .queryParam("radius", request.getRadius())
            .queryParam("key", apiKey())
            .build()
            .toUriString();
    log.info("search url: {}", url);

    ResponseEntity<String> result = restTemplate.getForEntity(url, String.class);

    HttpStatus statusCode = result.getStatusCode();
    log.info("search result: {}", result.getBody());
    if (statusCode.is2xxSuccessful()) {
        return true;
    } else {
        log.error("search failed, HTTP code: {}", statusCode.value());
        return false;
    }
}

} }

I'm unsure if you've figured this out yet, but I had the same issue tonight.我不确定你是否已经解决了这个问题,但今晚我遇到了同样的问题。 I noticed that the only thing encoded in the URI we were using was the comma between longitude and latitude in the location key.我注意到我们使用的 URI 中唯一编码的是位置键中经度和纬度之间的逗号。

I fixed my issue and got a good response when I used an actual comma instead of using %2C in the location values.当我在位置值中使用实际逗号而不是使用%2C时,我解决了我的问题并得到了很好的回应。

So rather than: location=48.1317547%2C11.5694659 , try using location=48.1317547,11.5694659 .因此,而不是: location=48.1317547%2C11.5694659 ,尝试使用location=48.1317547,11.5694659

Something else I ran into was the double dropping 0's from the end of the coordinates, so if that's something you hadn't thought through, make sure to use a DecimalFormat or something to always populate 6 decimal places.我遇到的其他问题是从坐标末尾双倍删除 0,因此如果您没有考虑到这一点,请确保使用 DecimalFormat 或其他始终填充小数点后 6 位的东西。

Happy Hacking!快乐黑客!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请求错误(invalid_request)Spring Rest API - request error (invalid_request) spring rest api Angular 8应用程序调用Springboot应用程序返回空 - angular 8 application calling Springboot application returns empty Google App Engine 忽略 Springboot 应用程序属性 - Google App Engine ignoring Springboot application properties 如何将 SpringBoot 应用程序部署到 Google App Engine? - How to deploy a SpringBoot Application to Google App Engine? 如何处理请求 url-SpringBoot 中的无效/额外特殊字符 & =? - How to handle invalid/extra special characters & = in request url-SpringBoot? 使用邮递员发布请求后,springboot 服务器返回错误 400 - springboot server returns error 400 after post request using postman FeignClient与WAR部署的SpringBoot应用程序的HTTP错误无效 - FeignClient Invalid HTTP error with WAR-deployed SpringBoot application System.currentTimeMillis() 在 Kotlin Springboot 应用程序中返回 0L - System.currentTimeMillis() Returns 0L in Kotlin Springboot Application 在多模块 springboot 应用中使用 Request Response Filter - Using Request Response Filter in a multi-module springboot application 如何在 lambda function 中为 Springboot CRUD 应用程序构建请求处理程序 class? - How to build request handler class in lambda function for a Springboot CRUD Application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM