I'm using merriam webster API and for some words, i get this error. I tried to see the difference between words and found that some of them are missing a property, in this case It's app-shortdef, which is a POJO class that i've created.
As I said, It only occurs on words that misses this property. Is there a way to just ignore these words?
Word that doens't work
"meta": {
"id": "car park",
"uuid": "c54c3476-f520-457e-97db-355b4fad1a0f",
"src": "learners",
"section": "alpha",
"target": {
"tuuid": "f675a703-73a4-4ba0-bab2-b516be9c9d5a",
"tsrc": "collegiate"
},
"stems": [
"car park",
"car parks"
],
"app-shortdef": [],
"offensive": false
},
Word that works
"meta": {
"id": "ball:2",
"uuid": "908a56c8-aa20-440d-bd0c-735d956ff1f4",
"src": "learners",
"section": "alpha",
"target": {
"tuuid": "be866f14-c794-4dee-bc3b-aee0002daaaa",
"tsrc": "collegiate"
},
"stems": [
"ball",
"balled",
"balling",
"balls"
],
"app-shortdef": {
"hw": "ball:2",
"fl": "verb",
"def": [
"{bc} to form (something) into a ball"
]
},
"offensive": false
},
As you can see, the difference is that app-shortdef is empty in the first one. Here is my POJO structure:
Dicionario
package com.ankitoword.entity;
public class Dicionario {
private Meta meta;
public Meta getMeta() {
return meta;
}
public void setMeta(Meta meta) {
this.meta = meta;
}
}
Meta
public class Meta {
private String id;
@JsonProperty(value="app-shortdef")
private AppShortDef appShortdef;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public AppShortDef getAppShortdef() {
return appShortdef;
}
public void setAppShortdef(AppShortDef appShortdef) {
this.appShortdef = appShortdef;
}
}
App
public class AppShortDef {
public String hw;
public String fl;
public String[] def;
public String getHw() {
String[] split = hw.split(":");
return split[0];
}
public void setHw(String hw) {
this.hw = hw;
}
public String getFl() {
return fl;
}
public void setFl(String fl) {
this.fl = fl;
}
public String[] getDef() {
return def;
}
public void setDef(String[] def) {
this.def = def;
}
}
Full Error
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Nov 26 19:24:58 BRT 2020
There was an unexpected error (type=Internal Server Error, status=500).
JSON decoding error: Cannot deserialize instance of `com.ankitoword.entity.AppShortDef` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.ankitoword.entity.AppShortDef` out of START_ARRAY token at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.ankitoword.entity.Dicionario["meta"]->com.ankitoword.entity.Meta["app-shortdef"])
org.springframework.core.codec.DecodingException: JSON decoding error: Cannot deserialize instance of `com.ankitoword.entity.AppShortDef` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.ankitoword.entity.AppShortDef` out of START_ARRAY token
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.ankitoword.entity.Dicionario["meta"]->com.ankitoword.entity.Meta["app-shortdef"])
at org.springframework.http.codec.json.AbstractJackson2Decoder.processException(AbstractJackson2Decoder.java:215)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
Service Class The error occurs on the collectList() line.
public List<Dicionario> getWords(String palavra) {
// OMono permite tratar o retorno quando a requisição finalizar sem bloquear o método.
Flux<Dicionario> fluxDicionario = this.webClient
.get()
.uri(builder -> builder.path("/"+palavra).queryParam("key", APIKey).build())
.retrieve() //Retorna o response-spec
.bodyToFlux(Dicionario.class);
List<Dicionario> dicionarios = new ArrayList<>();
dicionarios = fluxDicionario.collectList().block();
return dicionarios;
}
@Edit I've fixed this problem accepting a Object as a parameter, then checking if It's a array list, meaning It's the empty array and can't be converted.
public void setAppShortDef(Object object) {
if (object instanceof ArrayList){
//
} else {
ObjectMapper mapper = new ObjectMapper();
AppShortDef appShortDef = mapper.convertValue(object, AppShortDef.class);
this.appShortDef = appShortDef;
}
}
I Have another problem now, I can get a response that doesn't contain any of my properties. Ex:
[
"testified",
"the last\/final word",
"testify for",
"Lakewood",
"foreword",
"headword",
"lakewood",
"sent word",
"swearword",
"testament",
"testamentary",
"testaments",
"tested",
"tester",
"testers",
"testier",
"testosterone",
"the f-word",
"the final word",
"true to her word"
]