gpt4 book ai didi

java - Jackson 数据绑定(bind) MismatchedInputException

转载 作者:行者123 更新时间:2023-11-30 01:52:37 26 4
gpt4 key购买 nike

使用 spring 配置将数据从 json 读取到 spring bean 中,它适用于简单类型,但在尝试读取数组时出现上述错误这是我的配置

@Bean
Container container() throws IOException {

ObjectMapper objectMapper = new ObjectMapper();

return objectMapper.readValue(getClass().getClassLoader().getResource("book.json"), Container.class);
}

我决定按照建议放入一个容器,所以我的配置看起来像这样可以吗?因为我将使用这些数据来创建一些 Spring Controller 。POJO类

public class Container {
String requestedUrl;
List<Item> items;}

这是json数据,

{
"requestedUrl": "https://www.googleapis.com/books/v1/volumes?q=java&maxResults=40",
"items": [
{
"kind": "books#volume",
"id": "7tkN1CYzn2cC",
"etag": "pfjjxSpetIM",
"selfLink": "https://www.googleapis.com/books/v1/volumes/7tkN1CYzn2cC",
"volumeInfo": {
"title": "A Hypervista of the Java Landscape",
"publisher": "InfoStrategist.com",
"industryIdentifiers": [
{
"type": "ISBN_13",
"identifier": "9781592432172"
},
{
"type": "ISBN_10",
"identifier": "1592432174"
}
],
"readingModes": {
"text": true,
"image": true
},
"printType": "BOOK",
"maturityRating": "NOT_MATURE",
"allowAnonLogging": false,
"contentVersion": "1.0.1.0.preview.3",
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?id=7tkN1CYzn2cC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?id=7tkN1CYzn2cC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.pl/books?id=7tkN1CYzn2cC&pg=PP1&dq=java&hl=&cd=1&source=gbs_api",
"infoLink": "http://books.google.pl/books?id=7tkN1CYzn2cC&dq=java&hl=&source=gbs_api",
"canonicalVolumeLink": "https://books.google.com/books/about/A_Hypervista_of_the_Java_Landscape.html?hl=&id=7tkN1CYzn2cC"
},
"saleInfo": {
"country": "PL",
"saleability": "NOT_FOR_SALE",
"isEbook": false
}, //Shortened for brevity

如何编辑配置以使其正常工作

最佳答案

您正在传递以 {} 为界的 json 对象,但尝试将其转换为数组。使用此 json,您应该转换为另一个类,例如

public class Container {
private String requestedUrl;
private List<Item> items;

// getters/setters
}

关于java - Jackson 数据绑定(bind) MismatchedInputException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55560818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com