Пытаюсь получить ответ от погодной апишки апишка на запрос координат города присылает такой ответ
[
{
"name": "Moscow",
"local_names": {
"it": "Mosca",
"zh": "莫斯科",
"lv": "Maskava",
"id": "Moskwa",
"ay": "Mosku",
"lt": "Maskva",
"ascii": "Moscow",
"sh": "Moskva",
"uz": "Moskva",
"st": "Moscow",
"co": "Moscù",
"bo": "མོ་སི་ཁོ།",
"fi": "Moskova",
"wo": "Mosku",
"dz": "མོསི་ཀོ",
"sl": "Moskva",
"ps": "مسکو",
"sc": "Mosca",
"hr": "Moskva",
"kv": "Мӧскуа",
"tk": "Moskwa",
"sr": "Москва",
"mg": "Moskva",
"ko": "모스크바",
"wa": "Moscou",
"sg": "Moscow",
"sv": "Moskva",
"ch": "Moscow",
"mn": "Москва",
"ta": "மாஸ்கோ",
"am": "ሞስኮ",
"an": "Moscú",
"io": "Moskva",
"su": "Moskwa",
"ro": "Moscova",
"oc": "Moscòu",
"feature_name": "Moscow",
"bg": "Москва",
"nn": "Moskva",
"en": "Moscow",
"li": "Moskou",
"ga": "Moscó",
"ak": "Moscow",
"ba": "Мәскәү",
"da": "Moskva",
"so": "Moskow",
"ru": "Москва",
"be": "Масква",
"is": "Moskva",
"eo": "Moskvo",
"cu": "Москъва",
"uk": "Москва",
"cs": "Moskva",
"sk": "Moskva",
"vo": "Moskva",
"ab": "Москва",
"qu": "Moskwa",
"kg": "Moskva",
"cy": "Moscfa",
"az": "Moskva",
"pl": "Moskwa",
"jv": "Moskwa",
"fo": "Moskva",
"ia": "Moscova",
"lg": "Moosko",
"hu": "Moszkva",
"vi": "Mát-xcơ-va",
"eu": "Mosku",
"mt": "Moska",
"nb": "Moskva",
"pt": "Moscou",
"tl": "Moscow",
"ar": "موسكو",
"he": "מוסקווה",
"gv": "Moscow",
"sw": "Moscow",
"de": "Moskau",
"gd": "Moscobha",
"ja": "モスクワ",
"yo": "Mọsko",
"dv": "މޮސްކޯ",
"cv": "Мускав",
"bi": "Moskow",
"fa": "مسکو",
"gn": "Mosku",
"no": "Moskva",
"iu": "ᒨᔅᑯ",
"na": "Moscow",
"bs": "Moskva",
"kk": "Мәскеу",
"ml": "മോസ്കോ",
"zu": "IMoskwa",
"za": "Moscow",
"ca": "Moscou",
"hi": "मास्को",
"ln": "Moskú",
"la": "Moscua",
"mi": "Mohikau",
"av": "Москва",
"gl": "Moscova - Москва",
"es": "Moscú",
"os": "Мæскуы",
"af": "Moskou",
"se": "Moskva",
"ht": "Moskou",
"nl": "Moskou",
"kn": "ಮಾಸ್ಕೋ",
"yi": "מאסקווע",
"ty": "Moscou",
"br": "Moskov",
"el": "Μόσχα",
"ce": "Москох",
"tt": "Мәскәү",
"tr": "Moskova",
"ku": "Moskow",
"te": "మాస్కో",
"ie": "Moskwa",
"sq": "Moska",
"et": "Moskva",
"ss": "Moscow",
"hy": "Մոսկվա",
"tg": "Маскав",
"kl": "Moskva",
"kw": "Moskva",
"mr": "मॉस्को",
"ka": "მოსკოვი",
"ky": "Москва",
"fr": "Moscou",
"my": "မော်စကိုမြို့",
"sm": "Moscow",
"fy": "Moskou",
"mk": "Москва",
"ms": "Moscow",
"th": "มอสโก",
"ug": "Moskwa",
"ur": "ماسکو",
"bn": "মস্কো"
},
"lat": 55.7504461,
"lon": 37.6174943,
"country": "RU",
"state": "Moscow"
}
]
но в ответ получаю такой эксепшен
Exception in thread "main" org.springframework.web.client.RestClientException: Error while extracting response for type [class com.romanperkov.spring.rest.entity.cord] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.romanperkov.spring.rest.entity.cord` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.romanperkov.spring.rest.entity.cord` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:120)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:741)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:315)
at com.romanperkov.spring.rest.Communication.test(Communication.java:81)
at com.romanperkov.spring.rest.App.main(App.java:35)
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.romanperkov.spring.rest.entity.cord` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.romanperkov.spring.rest.entity.cord` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:284)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:242)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:105)
... 5 more
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.romanperkov.spring.rest.entity.cord` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1468)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1242)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1190)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:604)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:190)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:166)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4526)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3521)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:273)
... 7 more
Process finished with exit code 1
Я так понял, что проблема в том что ответ апишки начинается с квадратных скобок , то есть она возвращает массив, но он без имени , как в таких случаях действовать?
класс описывающий сущность которую надо получить из апи респонса
@Getter
@Setter
@NoArgsConstructor
public class cord {
private List<String> name;
}
Решение проблемы заключается в том что бы указать класс как массив
String url="http://api.openweathermap.org/geo/1.0/direct?q=QWERTY&limit=5&appid=fe7e6bcec3fedc63d4a158abd1c3c3eb";
cord[] responseEntity=restTemplate.getForObject
(url.replace("QWERTY","москва"), cord[].class);
System.out.println(responseEntity[0]);