Spring Boot Deserialiserung mit JSON

OnDemand

Top Contributor
Hallo zusammen,

ich kann eine API REsponse nicht in Object mappen, da die API Absätze und Leerzeichen ausgibt
1629360933226.png
Ziehmlich bescheiden. Hat jemand ne bessere Idee als die untenstehende?

Java:
if(response.getStatusCode().is2xxSuccessful()){
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
            objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
            objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
            try {
                String string=response.getBody().replaceAll("\\\n","").replaceAll(" ","");
                return objectMapper.readValue(string, TaxClassResponse.class);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
 
Java:
org.springframework.web.client.RestClientException: Error while extracting response for type [class com.app.modules.exporter.entities.categories.CategoryResponse] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.app.modules.exporter.entities.categories.CategoryResponse` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.app.modules.exporter.entities.categories.CategoryResponse` out of START_ARRAY token
 at [Source: (ByteArrayInputStream); line: 1, column: 1]

Java:
[
    {
        "id": 59,
        "parentId": 0,
        "isActive": true,
        "name": "xxx",
        "headingTitle": "xxx",
        "description": "",
        "urlKeywords": "",
        "icon": "",
        "image": "",
        "imageAltText": "",
        "_links": []
    }
]

Java:
public class CategoryResponse {

    @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
    private List<CategoryDetailed> categories;

    public List<CategoryDetailed> getCategories() {
        return categories;
    }

    public void setCategories(List<CategoryDetailed> categories) {
        this.categories = categories;
    }

}
 
Für mich sieht es eher danach aus, dass der Unmarshaller ein Array als type erwartet, Du ihm aber das Object CategoryResponse übergibst.
Das würde dann auch die Fehlermeldung erklären.
Java:
Cannot deserialize instance of `com.app.modules.exporter.entities.categories.CategoryResponse` out of START_ARRAY token

Versuche es mal so.
Java:
List<CategoryDetailed> asList = objectMapper.readValue(response.getBody(), List.class);
 
Aber warum klappt das nicht, wenn ich es Ohne ObjectMapper machen will? Aahh jetzt hats klick gemacht!
Das Array hat keinen Namen. In anderen API heisst es zb

Java:
"data":

[
    {
        "id": 59,
        "parentId": 0,
        "isActive": true,
        "name": "xxx",
        "headingTitle": "xxx",
        "description": "",
        "urlKeywords": "",
        "icon": "",
        "image": "",
        "imageAltText": "",
        "_links": []
    }
]
 
Warum funktioniert das? Ich werd bekloppt. DANKE
Weil Dein JSON mit einem Array Token startet [, nicht mit einem Object Token {.
Das Array hat keinen Namen. In anderen API heisst es zb
Dann würde aber das JSON so aussehen und dann wäre data ja ein Member des root objects. Dann würde Dein Object CategoryResponse auch funktionieren, sofern Du die Membervariable in data umbenennst.
Java:
{
    "data":
    [
        {
            "id": 59,
            "parentId": 0,
            "isActive": true,
            "name": "xxx",
            "headingTitle": "xxx",
            "description": "",
            "urlKeywords": "",
            "icon": "",
            "image": "",
            "imageAltText": "",
            "_links": []
        }
    ]
}

Edit: Es sollte dann aber auch ohne ObjectMapper funktionieren.
 

Zurück
Oben