映射到POJO的ObjectMapper返回Null

时间:2019-04-18 20:56:28

标签: java json spring-boot jackson

我有一个简单的Json,格式如下:

{
  "products": [
    {
      "id": 1,
      "productSku": "123",
      "productInventory": "24"
    },
    {
      "id": 2,
      "productSku": "12350",
      "productInventory": "22"
    },
    {
      "id": 3,
      "productSku": "12351",
      "productInventory": "19"
    }
  ]
}

我正在通过本地主机URL捕获此Json,并尝试将其映射到在不同端口上运行的另一个Spring Boot应用程序中的POJO类。与上述Json对应的POJO类为:

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName(value = "products")
public class Product {

    private Integer id;
    private String productSku;
    private String productInventory;

    //getters and setters omitted


}

以下是我处理映射和绑定的控制器:

@ResponseBody
    @GetMapping("index")
    public String getIndex() throws JsonParseException, 
       JsonMappingException, IOException {

        URL ApiUrlJson = new URL("http://localhost:1990/product");

        ObjectMapper mapper = new ObjectMapper();

        Product product = mapper.readValue(ApiUrlJson, Product.class);
        System.out.println(product.getProductInventory());

        return "Processed!";
    }

当我运行应用程序和访问索引以获取某个变量的值时,输出始终为null,我不确定自己缺少什么吗?

3 个答案:

答案 0 :(得分:2)

您的JSON文件中有一个产品列表。您正在反序列化具有3个字段的一个对象。这些字段都不在产品下,但是您会忽略缺少的字段。

尝试反序列化为包含产品列表的新类。 删除@JsonRootName

答案 1 :(得分:1)

您正在尝试反序列化JSON数组。另外,由于此变量的访问权限是私有的,因此请不要省略设置方法。您可以参考下面的代码。我已经检查了它,并能够对其进行反序列化。

@JsonIgnoreProperties(ignoreUnknown = true)
class Product {

  private Integer id;
  private String productSku;
  private String productInventory;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getProductSku() {
    return productSku;
  }

  public void setProductSku(String productSku) {
    this.productSku = productSku;
  }

  public String getProductInventory() {
    return productInventory;
  }

  public void setProductInventory(String productInventory) {
    this.productInventory = productInventory;
  }
}

class Products{

  private List<Product> products;

  public List<Product> getProducts() {
    return products;
  }

  public void setProducts(List<Product> products) {
    this.products = products;
  }
}

public class Main{

  public static void main(String[] args) throws Exception{
    String jsonString =
        "{\n"
            + "  \"products\": [\n"
            + "    {\n"
            + "      \"id\": 1,\n"
            + "      \"productSku\": \"123\",\n"
            + "      \"productInventory\": \"24\"\n"
            + "    },\n"
            + "    {\n"
            + "      \"id\": 2,\n"
            + "      \"productSku\": \"12350\",\n"
            + "      \"productInventory\": \"22\"\n"
            + "    },\n"
            + "    {\n"
            + "      \"id\": 3,\n"
            + "      \"productSku\": \"12351\",\n"
            + "      \"productInventory\": \"19\"\n"
            + "    }\n"
            + "  ]\n"
            + "}";

    ObjectMapper mapper = new ObjectMapper();
    Products products = mapper.readValue(jsonString, Products.class);
    // To just access list of ids
    List<Product> productsList= products.getProducts();
    List<Integer> productIds = new ArrayList<>();
    for(Product product : productsList){
      productIds.add(product.getId());
    }
    System.out.println(productIds);
    }
}

答案 2 :(得分:0)

您有一个对象,并且在该对象内部是对象列表。即

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName(value = "products")
public class Products {
    private List<Product> products;

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(final List<Product> products) {
        this.products = products;
    }
}


public class Product{
.....
}

ObjectMapper objectMapper = new ObjectMapper();
Products product = objectMapper.readValue("json here....", Products.class);
System.out.println(product.getProducts().size());