Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 来自Jackson库的无法识别的属性异常_Java_Jackson_Jackson Databind - Fatal编程技术网

Java 来自Jackson库的无法识别的属性异常

Java 来自Jackson库的无法识别的属性异常,java,jackson,jackson-databind,Java,Jackson,Jackson Databind,我有如下JSONpaylaod: { "status": "ok", "result": { "result": [ { "product_id": "1420-131617-82", "sku": "1420-131617", "display_sku": "8DD 355 100-411", "genart_number": 82, "name": "Bremsscheibe",

我有如下
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();
    }
我得到以下例外情况:

在[来源: (字符串)“{”产品id:“1420-131617-82”,“sku:“1420-131617”,“显示sku:“8DD” 355 100-411,“genart_编号”:82,“名称”:“勃拉姆斯基”,“描述”:null,“附加信息文本”:null,“产品url”:“图像url”:“品牌”:“HELLA” PAGID,“eans”:[“408230365078”],“制造商产品编号”:“8DD 355” 100-411,“数据供应商编号”:“4625”,“价格锤”:假,“可购买”:tr“[截断 174个字符];行:1,列:16](通过引用链: com.kfz24.mockingservice.mapper.SimpleproductDtoMapper[“产品id”]) 在 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61) 在 com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823) 在 com.fasterxml.jackson.databind.desr.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153) 在 com.fasterxml.jackson.databind.deser.beandserializerbase.handleUnknownProperty(beandserializerbase.java:1589) 在 com.fasterxml.jackson.databind.deser.beandserializerbase.handleUnknownVanilla(beandserializerbase.java:1567) 在 com.fasterxml.jackson.databind.deser.BeandSerializer.vanillaDeserialize(BeandSerializer.java:294) 在 deserialize(beandSerializer.java:151) 在 com.fasterxml.jackson.databind.ObjectMapper.\u readMapAndClose(ObjectMapper.java:4013) 位于com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004) 位于com.kfz24.mockingservice.service.impl.MockingProductServiceImpl.getProducts(MockingProductServiceImpl.java:49) 位于com.kfz24.mockingservice.controller.MockingProductController.getProducts(MockingProductController.java:29)


因为
@SerializedName
来自Gson,而不是Jackson。Jackson中的等价物是
@JsonProperty

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


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

,因为
@SerializedName
来自Gson,而不是Jackson。Jackson中的等价物是
@JsonProperty

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

因此,对于您来说,我建议最简单的方法是确保所有字段都标记有
@JsonProperty
(例如名称、品牌等)使用可以生成带有
Jackson
注释的
POJO
类。选择
Jackson 2.x
以生成带有
Jackson
注释的模型。
源类型应为
JSON
。下面可以看到我使用此工具生成的
产品
类。所有属性都是
公共属性
,因此将它们更改为
private
,并生成
getter
setter

class Product {

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

    @Override
    public String toString() {
        return "Product{" +
                "productId='" + productId + '\'' +
                ", sku='" + sku + '\'' +
                ", displaySku='" + displaySku + '\'' +
                ", genartNumber=" + genartNumber +
                ", name='" + name + '\'' +
                ", description=" + description +
                ", additionalInfoText=" + additionalInfoText +
                ", productUrl='" + productUrl + '\'' +
                ", imageUrl='" + imageUrl + '\'' +
                ", brand='" + brand + '\'' +
                ", eans=" + eans +
                ", manufacturerProductNumber='" + manufacturerProductNumber + '\'' +
                ", dataSupplierNumber='" + dataSupplierNumber + '\'' +
                ", pricehammer=" + pricehammer +
                ", buyable=" + buyable +
                ", bulkyGood=" + bulkyGood +
                ", riskyGood=" + riskyGood +
                ", hazardousGood=" + hazardousGood +
                ", carSpecific=" + carSpecific +
                ", hasDeposit=" + hasDeposit +
                ", isExchangePart=" + isExchangePart +
                ", visibilityStatus='" + visibilityStatus + '\'' +
                ", deleted=" + deleted +
                '}';
    }
}
以上代码打印:

[Product{productId='1420-131617-82', sku='1420-131617', displaySku='8DD 355 100-411', genartNumber=82, name='Bremsscheibe', description=null, additionalInfoText=null, productUrl='https://www.autoteile5000.de/product/1420-131617-82', imageUrl='https://static.autoteile5000.de/product-images/HLP/4625-462502682-3-255-1548045462267.jpg', brand='HELLA PAGID', eans=[4082300365078], manufacturerProductNumber='8DD 355 100-411', dataSupplierNumber='4625', pricehammer=false, buyable=true, bulkyGood=false, riskyGood=false, hazardousGood=false, carSpecific=true, hasDeposit=false, isExchangePart=false, visibilityStatus='active', deleted=false}]
使用可以生成带有
Jackson
注释的
POJO
类。选择
Jackson 2.x
以生成带有
Jackson
注释的模型。
源类型应为
JSON
。下面可以看到使用此工具生成的
Product
类。所有属性都是
public
>,因此将它们更改为
private
,并生成
getter
setter

class Product {

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

    @Override
    public String toString() {
        return "Product{" +
                "productId='" + productId + '\'' +
                ", sku='" + sku + '\'' +
                ", displaySku='" + displaySku + '\'' +
                ", genartNumber=" + genartNumber +
                ", name='" + name + '\'' +
                ", description=" + description +
                ", additionalInfoText=" + additionalInfoText +
                ", productUrl='" + productUrl + '\'' +
                ", imageUrl='" + imageUrl + '\'' +
                ", brand='" + brand + '\'' +
                ", eans=" + eans +
                ", manufacturerProductNumber='" + manufacturerProductNumber + '\'' +
                ", dataSupplierNumber='" + dataSupplierNumber + '\'' +
                ", pricehammer=" + pricehammer +
                ", buyable=" + buyable +
                ", bulkyGood=" + bulkyGood +
                ", riskyGood=" + riskyGood +
                ", hazardousGood=" + hazardousGood +
                ", carSpecific=" + carSpecific +
                ", hasDeposit=" + hasDeposit +
                ", isExchangePart=" + isExchangePart +
                ", visibilityStatus='" + visibilityStatus + '\'' +
                ", deleted=" + deleted +
                '}';
    }
}
以上代码打印:

[Product{productId='1420-131617-82', sku='1420-131617', displaySku='8DD 355 100-411', genartNumber=82, name='Bremsscheibe', description=null, additionalInfoText=null, productUrl='https://www.autoteile5000.de/product/1420-131617-82', imageUrl='https://static.autoteile5000.de/product-images/HLP/4625-462502682-3-255-1548045462267.jpg', brand='HELLA PAGID', eans=[4082300365078], manufacturerProductNumber='8DD 355 100-411', dataSupplierNumber='4625', pricehammer=false, buyable=true, bulkyGood=false, riskyGood=false, hazardousGood=false, carSpecific=true, hasDeposit=false, isExchangePart=false, visibilityStatus='active', deleted=false}]

感谢您的回复..但是私有visibilityStatus到visibilityStatus是一个对象,@JsonProperty(“visibility\u status”)不适用于object?@Vinayanyak,在您的
JSON
payload
“visibility\u status”中:“活动”
是一个
字符串
而不是
对象
。这
JSON
是否正确?如果正确,请更新您的模型。
public enum VisibilityStatusDto{ACTIVE(1),INACTIVE(2);private Integer id;private VisibilityStatusDto(Integer id){this.id=id;}public Integer getId(){return this.id;}
这是一个枚举,但来自其他服务的json负载将其作为字符串发送。我如何将此字符串映射到mu模型中的枚举类型?请查看一下。让我知道结果是什么。感谢帮助!!感谢您的回复..但是private VisibilityStatusDto visibilityStatus是一个对象和@JsonProperty(“可见性状态”对对象不起作用?@VinayaNayak,在您的
JSON
payload
“可见性状态”中:“活动的”
是一个
字符串,而不是
Object
。这
JSON
正确吗?如果正确,请更新您的模型。
public enum VisibilityStatusDto{active(1),INACTIVE(2);private Integer id;private VisibilityStatusDto(Integer id){this.id=id;}public Integer getId(){return this.id;}}
这是一个枚举,但来自其他服务的json负载将其作为字符串发送。如何将此字符串映射到mu模型中的枚举类型?请查看此项。让我知道结果。感谢帮助!!