gpt4 book ai didi

java - 来自 Jackson 库的 UnrecognizedPropertyException

转载 作者:行者123 更新时间:2023-12-02 01:38:08 24 4
gpt4 key购买 nike

我有 JSON paylaod 如下:

{
"status": "ok",
"result": {
"result": [
{
"product_id": "1420-131617-82",
"sku": "1420-131617",
"display_sku": "8DD 355 100-411",
"genart_number": 82,
"name": "Bremsscheibe",
"description": null,
"additional_info_text": null,
"product_url": "https://www.autoteile5000.de/product/1420-131617-82",
"image_url": "https://static.autoteile5000.de/product-images/HLP/4625-462502682-3-255-1548045462267.jpg",
"brand": "HELLA PAGID",
"eans": [
"4082300365078"
],
"manufacturer_product_number": "8DD 355 100-411",
"data_supplier_number": "4625",
"pricehammer": false,
"buyable": true,
"bulky_good": false,
"risky_good": false,
"hazardous_good": false,
"car_specific": true,
"has_deposit": false,
"is_exchange_part": false,
"visibility_status": "active",
"deleted": false
}
]
}
}

这是我反序列化它的方法:

public List<SimpleProductDto> getProducts(ProductForm productForm) {

JsonParser jsonParser = new JsonParser();
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("data/product/2210-0929-818/product.json") ) {
String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
//System.out.println("print text : " + text);
//Read JSON file
JsonObject objectFromString = jsonParser.parse(text).getAsJsonObject();
// System.out.println(objectFromString.toString());

JsonObject objectFromString1 = objectFromString.getAsJsonObject("result");
// System.out.println(objectFromString.toString());

// System.out.println(objectFromString1.toString());

JsonArray jsonArray = objectFromString1.getAsJsonArray("result");
System.out.println("printing json array : " +jsonArray.toString());

ObjectMapper oMapper = new ObjectMapper();
for(JsonElement element : jsonArray){
JsonObject productObj = element.getAsJsonObject();
System.out.println("printing json object : " + productObj.toString());
SimpleproductDtoMapper productDtoList = oMapper.readValue(productObj.toString(), SimpleproductDtoMapper.class);
}
// List<SimpleproductDtoMapper> productDtoList = oMapper.readValue(jsonArray.toString(), new TypeReference<List<SimpleproductDtoMapper>>() {});

// Map<String, SimpleproductDtoMapper> items = productDtoList.stream().collect(Collectors.toMap(SimpleproductDtoMapper::getProductId, Function.identity()));

//items.forEach((k, v) -> System.out.println("Item : " + k + " Count : " + v));

//Iterate over employee array
//productList.forEach(emp -> parseProductObject((JSONObject) emp));

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

这是我的POJO:

public class SimpleproductDtoMapper {

@SerializedName("product_id")
private String productId;
private String sku;
@SerializedName("display_sku")
private String displaySku;
@SerializedName("genart_number")
private Integer genartNumber;
private String name;
private String description;
@SerializedName("additional_info_text")
private String additionalInfoText;
@SerializedName("product_url")
private String productUrl;
@SerializedName("image_url")
private String imageUrl;
private String brand;
private List<String> eans;
@SerializedName("manufacturer_product_number")
private String manufacturerProductNumber;
@SerializedName("data_supplier_number")
private String dataSupplierNumber;
private boolean pricehammer;
private boolean buyable;
@SerializedName("bulky_good")
private boolean bulkyGood;
@SerializedName("risky_good")
private boolean riskyGood;
@SerializedName("hazardous_good")
private boolean hazardousGood;
@SerializedName("car_specific")
private boolean carSpecific;
@SerializedName("has_deposit")
private boolean hasDeposit;
@SerializedName("is_exchange_part")
private boolean isExchangePart;
@SerializedName("visibility_status")
private VisibilityStatusDto visibilityStatus;
@SerializedName("deleted")
private boolean deleted;
}

我得到以下异常:

at [Source: (String)"{"product_id":"1420-131617-82","sku":"1420-131617","display_sku":"8DD 355 100-411","genart_number":82,"name":"Bremsscheibe","description":null,"additional_info_text":null,"product_url":"https://www.autoteile5000.de/product/1420-131617-82","image_url":"https://static.autoteile5000.de/product-images/HLP/4625-462502682-3-255-1548045462267.jpg","brand":"HELLA PAGID","eans":["4082300365078"],"manufacturer_product_number":"8DD 355 100-411","data_supplier_number":"4625","pricehammer":false,"buyable":tr"[truncated 174 chars]; line: 1, column: 16] (through reference chain: com.kfz24.mockingservice.mapper.SimpleproductDtoMapper["product_id"]) at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61) at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823) at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004) at com.kfz24.mockingservice.service.impl.MockingProductServiceImpl.getProducts(MockingProductServiceImpl.java:49) at com.kfz24.mockingservice.controller.MockingProductController.getProducts(MockingProductController.java:29)

最佳答案

因为 @SerializedName 来自 Gson 而不是 Jackson 。Jackson 中的等效项是 @JsonProperty

因此将所有@SerializedName更改为@JsonProperty。另外,Jackson 默认情况下仅反序列化未用 @JsonProperty 标记的公共(public)字段。

所以对你来说,我建议最简单的方法是确保所有字段都用 @JsonProperty 标记(例如名称、品牌等)

关于java - 来自 Jackson 库的 UnrecognizedPropertyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54890696/

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