我想一次从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,但不起作用。
使String成为ResponseEntity只能返回我期望的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"}}
将其设置为String []会给我一个406不可接受的错误。
ResponseEntity<String> response = this.restTemplate.exchange(
uri,
HttpMethod.GET,
null,
new ParameterizedTypeReference<String>() {}
);
当我尝试使用地址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;
答案 0 :(得分:0)
我知道了。以下作品:
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参数可以解决此问题。