使用 Spring RestTemplate 发送获取请求 returns 一个数组

Sending a get request with Spring RestTemplate that returns an array

我想一次从 Nominatim 获取 50 个随机地址用于数据生成。我在不使用 Spring Framework 的情况下工作,但我需要使用 Spring RestTemplate 来实现它。

每秒获得 50 个非常重要,因为 Nominatim 相当慢,我希望能够生成大量数据。

一次给我 50 个回复:

public void processRequest() throws IOException, URISyntaxException {
        do {
            HttpGet httpGet = new HttpGet(getURI());
            CloseableHttpResponse response = httpclient.execute(httpGet);
            try {
                this.responseAsString = responseToJsonString(response);
            } finally {
                response.close();
            }
        } while (this.responseAsString.equals(INVALID_LAT_LONG_RESPONSE));

我试过关注 this tutorial 但没用。

仅使字符串类型的 ResponseEntity returns 成为我期望的 50 的第一个值。

{"place_id":78213552,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":10055484,"lat":"32.920202","lon":"-97.528753","display_name":"Charles Avenue, Azle, Tarrant County, Texas, 76020, USA","class":"highway","type":"residential","importance":0.1,"address":{"road":"Charles Avenue","town":"Azle","county":"Tarrant County","state":"Texas","postcode":"76020","country":"USA","country_code":"us"}}

将其设为字符串[] 会出现 406 不可接受的错误。

ResponseEntity<String> response = this.restTemplate.exchange(
                uri,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<String>() {}
                );

当我尝试使用 Address POJO 和尝试为该类型使用 Address[] 时,我遇到了相同的 406 错误。

这是地址 POJO 中的字段。每个字段都有一个 getter 和一个 setter(名称只是 Eclipse 提供的默认名称,因此它不会导致 Jackson 的解析出现问题)。

package com.bottomline.ml.generator.nominatimRequest;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Address {

    @JsonProperty("place_id")
    private long placeId;

    @JsonProperty("licence")
    private String licence;

    @JsonProperty("osm_type")
    private String osmType;

    @JsonProperty("osm_id")
    private String osmId;

    @JsonProperty("lon")
    private double longitude;

    @JsonProperty("lat")
    private double latitude;

    @JsonProperty("display_name")
    private String displayName;

    @JsonProperty("class")
    private String elementClass;

    @JsonProperty("type")
    private String elementType;

    @JsonProperty("importance")
    private double importance;

    @JsonProperty("address")
    private String addressDetails;

我明白了。以下作品:

HashMap<String, Object> params = getParams();
ResponseEntity<String> response = this.restTemplate.getForEntity(SEARCH, String.class, params);

其中 getParams() 是:

    private static HashMap<String, Object> getParams() {
        Random rand = new Random();
        String ids = "";
        for (int i = 0; i < MAX_REQUESTS; i++) {
            ids += "W" + Integer.toString(rand.nextInt(10000000) + 10000000) + ',';
        }

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("osmId", ids);
        params.put("format", "json");

        return params;
    }

我有以下字段:

    private static String osmId = "&osm_ids={osmId}";
    private static String format = "&format={format}";
    private static String SEARCH = <your favorite nominatim server> + osmId + format;

将 URI 转换为字符串时很可能出现问题,导致参数中的逗号被读取为 %2C,而不是逗号。 HashMap 参数可能修复了这个问题。