我正在尝试调用HERE geocoder API,但是在有效的url调用中,响应主体尚未完全初始化。
如下所示,我只需调用提供有效网址的API:
val url = UriComponentsBuilder.fromUriString(properties.geocodeUrl)
.queryParam("app_id", properties.appId)
.queryParam("app_code", properties.appCode)
.queryParam("searchtext", "10 Downing Street, London")
.toUriString()
logger.debug("calling geocoder rest service: $url")
val geocoderResponse = RestTemplate().getForObject<GeocoderResponse>(url)
GeocoderResponse
package com.gudjob.geolocation
import com.fasterxml.jackson.annotation.JsonProperty
import com.gudjob.core.domain.Coordinates
import java.io.Serializable
/**
* @author Elia Sgolmin
*/
internal data class GeocoderResponse(
@field:JsonProperty("Response")
var response: Response?
) : Serializable {
data class Response(
@field:JsonProperty("View")
var views: List<View>
) : Serializable {
data class View(
@field:JsonProperty("Result")
var results: List<Result>
) : Serializable {
data class Result(
@field:JsonProperty("Location")
var location: Location?
) : Serializable {
data class Location(
@field:JsonProperty("DisplayPosition")
var displayPosition: DisplayPosition?
) : Serializable {
data class DisplayPosition(
@field:JsonProperty("Latitude")
var latitude: Double?,
@field:JsonProperty("Longitude")
var longitude: Double?
) : Serializable {
fun toCoordinates(): Coordinates {
val lat = latitude
val lon = longitude
return if (lat == null || lat.isNaN() || lon == null || lon.isNaN())
Coordinates.Unavailable
else
Coordinates.Available(lat, lon)
}
}
}
}
}
}
}
如果我复制登录的URL并在浏览器或邮递员中使用它,则会得到以下预期响应
{
"Response":{
"MetaInfo":{
"Timestamp":"2019-02-26T14:03:09.236+0000"
},
"View":[
{
"_type":"SearchResultsViewType",
"ViewId":0,
"Result":[
{
"Relevance":1,
"MatchLevel":"houseNumber",
"MatchQuality":{
"City":1,
"Street":[
1
],
"HouseNumber":1
},
"MatchType":"pointAddress",
"Location":{
"LocationId":"NT_lWsc8knsFwVitNTFX88zmA_xAD",
"LocationType":"point",
"DisplayPosition":{
"Latitude":51.50341,
"Longitude":-0.12765
},
"NavigationPosition":[
{
"Latitude":51.50322,
"Longitude":-0.12767
}
],
"MapView":{
"TopLeft":{
"Latitude":51.5045342,
"Longitude":-0.129456
},
"BottomRight":{
"Latitude":51.5022858,
"Longitude":-0.125844
}
},
"Address":{
"Label":"10 Downing Street, London, SW1A 2AA, United Kingdom",
"Country":"GBR",
"State":"England",
"County":"London",
"City":"London",
"District":"Westminster",
"Street":"Downing Street",
"HouseNumber":"10",
"PostalCode":"SW1A 2AA",
"AdditionalData":[
{
"value":"United Kingdom",
"key":"CountryName"
},
{
"value":"England",
"key":"StateName"
},
{
"value":"London",
"key":"CountyName"
}
]
}
}
}
]
}
]
}
}
如您所见,“ Response”对象的“ View”属性是一个数组,其中一个对象包含给定地址的纬度和经度。
我得到的是
GeocoderResponse(响应=响应(观看次数= []))
response
已初始化,但views
为空。
更改我的代码以使用纯文本格式获取JSON响应
restTemplate().getForObject<String>(url)
它会产生以下json
{“ Response”:{“ MetaInfo”:{“ Timestamp”:“ 2019-02-26T13:35:37.851 + 0000”},“ View”:[]}}
即使在这里"View"
属性为空,因此我也不认为问题与JSON反序列化有关。
有什么建议吗?
我检查了RestTemplate
请求标头与浏览器和邮递员标头,但找不到任何相关的区别。
我唯一能指出的就是邮递员响应标头 Content-Encoding→gzip ,在Spring响应中缺少。
答案 0 :(得分:0)
问题可能是,通过UriComponentsBuilder接口请求时,查询参数searchtext没有经过URL编码。尝试对要传递的字符串进行编码,然后检查响应。
#include <iostream>
#include <string>
#include <cctype>
int main(){
for( int r = 0; r < 7; ++r ){
for(int c = 0; c<5;++c){
if ((c == 0) || (c == 4 && (r != 0 && r != 3)) || ((r == 0 || r == 3) && (c > 0 && c < 4))){
std::cout << "R" << "" ;
}
else{
std::cout << " ";
}
}
std::cout << std::endl;
}
return 0;
}