映射到 POJO 的 ObjectMapper 返回 Null

我有一个以下形式的简单 Json:


{

  "products": [

    {

      "id": 1,

      "productSku": "123",

      "productInventory": "24"

    },

    {

      "id": 2,

      "productSku": "12350",

      "productInventory": "22"

    },

    {

      "id": 3,

      "productSku": "12351",

      "productInventory": "19"

    }

  ]

}

我正在通过 localhost 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!";

    }

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


江户川乱折腾
浏览 480回答 3
3回答

潇湘沐

您的 JSON 文件中有一个产品列表。您正在反序列化一个具有 3 个字段的对象。这些字段都不是在产品下,但您忽略了缺少的字段。尝试反序列化为包含 Product 列表的新类。删除 @JsonRootName

侃侃无极

您正在尝试反序列化 JSON 数组。也不要省略 setter,因为这个变量的访问是私有的。你可以参考下面的代码。我已经检查过了,我能够正确地反序列化它。@JsonIgnoreProperties(ignoreUnknown = true)class Product {&nbsp; private Integer id;&nbsp; private String productSku;&nbsp; private String productInventory;&nbsp; public Integer getId() {&nbsp; &nbsp; return id;&nbsp; }&nbsp; public void setId(Integer id) {&nbsp; &nbsp; this.id = id;&nbsp; }&nbsp; public String getProductSku() {&nbsp; &nbsp; return productSku;&nbsp; }&nbsp; public void setProductSku(String productSku) {&nbsp; &nbsp; this.productSku = productSku;&nbsp; }&nbsp; public String getProductInventory() {&nbsp; &nbsp; return productInventory;&nbsp; }&nbsp; public void setProductInventory(String productInventory) {&nbsp; &nbsp; this.productInventory = productInventory;&nbsp; }}class Products{&nbsp; private List<Product> products;&nbsp; public List<Product> getProducts() {&nbsp; &nbsp; return products;&nbsp; }&nbsp; public void setProducts(List<Product> products) {&nbsp; &nbsp; this.products = products;&nbsp; }}public class Main{&nbsp; public static void main(String[] args) throws Exception{&nbsp; &nbsp; String jsonString =&nbsp; &nbsp; &nbsp; &nbsp; "{\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; \"products\": [\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; {\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; \"id\": 1,\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; \"productSku\": \"123\",\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; \"productInventory\": \"24\"\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; },\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; {\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; \"id\": 2,\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; \"productSku\": \"12350\",\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; \"productInventory\": \"22\"\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; },\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; {\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; \"id\": 3,\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; \"productSku\": \"12351\",\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; \"productInventory\": \"19\"\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; }\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; ]\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "}";&nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; Products products = mapper.readValue(jsonString, Products.class);&nbsp; &nbsp; // To just access list of ids&nbsp; &nbsp; List<Product> productsList= products.getProducts();&nbsp; &nbsp; List<Integer> productIds = new ArrayList<>();&nbsp; &nbsp; for(Product product : productsList){&nbsp; &nbsp; &nbsp; productIds.add(product.getId());&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(productIds);&nbsp; &nbsp; }}

慕姐8265434

您有一个对象,该对象内部是一个对象列表。IE@JsonIgnoreProperties(ignoreUnknown = true)@JsonRootName(value = "products")public class Products {&nbsp; &nbsp; private List<Product> products;&nbsp; &nbsp; public List<Product> getProducts() {&nbsp; &nbsp; &nbsp; &nbsp; return products;&nbsp; &nbsp; }&nbsp; &nbsp; public void setProducts(final List<Product> products) {&nbsp; &nbsp; &nbsp; &nbsp; this.products = products;&nbsp; &nbsp; }}public class Product{.....}ObjectMapper objectMapper = new ObjectMapper();Products product = objectMapper.readValue("json here....", Products.class);System.out.println(product.getProducts().size());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java