4

I use Spring-boot and RestTemplate. I'm having trouble translating from json to Dto.

Client

Items items = restTemplate.getForObject("https://xxxxx/xxxxx, Items.class);

Items

@Getter
@Setter
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = false)
public class Items {

    private List<Item> Items;

    public Items() {
        Items = new ArrayList<>();
    }

    @Override
    public String toString() {
        return String.valueOf(Items.size());
    }
}

Item

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Item {

    private String title;

    @Override
    public String toString() {
        return "Item{" + "title=" + title + '}';
    }

}

Json is here(formatted).

{
  "Items": [
    {
      "Item": {
        "limitedFlag": 0,
        "authorKana": "XXX",
        "author": "XXX",
        "subTitle": "XXX",
        "seriesNameKana": "XXXX",
        "title": "XXXX",
        "subTitleKana": "XXXX"
      }
    },
    {
      "Item": {
        "limitedFlag": 0,
        "authorKana": "XXX",
        "author": "XXX",
        "subTitle": "XXX",
        "seriesNameKana": "XXXX",
        "title": "XXXX",
        "subTitleKana": "XXXX"
      }
    }
  ],
  "last": 30
}

I receive this error message:

Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `[Ldev.itboot.mb.rest.Item;` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `[Ldev.itboot.mb.rest.Item;` from Object value (token `JsonToken.START_OBJECT`)

I looked here and on google but still cannot figure out.

Any response would be helpful.

Regards.

2
  • Check the json with jsonformatter.org , there is some error inside Commented Jun 4, 2021 at 7:52
  • @ samabcde Thank you for your adivice.As you mentioned, json was wrong.Now I reviced. Commented Jun 6, 2021 at 7:42

1 Answer 1

1

The end of your JSON seem incorrect, there should be a comma after the array, before "last".

  ]
  "last": 30
}
Sign up to request clarification or add additional context in comments.

1 Comment

yes, you are right, my check was lax. Now it is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.