0

I have the following code:

@RequestMapping(value = "/checkInventory", consumes = "application/json", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<?> checkInventory (
    // HEADERS
    @RequestBody CheckInventoryRequest checkInventoryRequest) throws Exception {

    // HEADERS LOGIC

    CheckInventoryResponse checkInventoryResponse  = new CheckInventoryResponse();

JSON

{
  "supplier": "foo",
  "supplierName": "ads",
  "supplierLocation": "123",
  "site": "goo",
  "product": [
    {
      "lineItem": 1,
      "manufacturer": "TEST",
      "partNumber": "123",
    },
    {
      "lineItem": 2,
      "manufacturer": "TEST 2",
      "partNumber": "3456",
    }
  ]
}

I am able to populate the POJO with the regular values, but how do I make another ArrayList using the product part?

I need to do some database checking with the JSON but I can't figure out how to save the ArrayList.

heckInventoryResponse

import java.io.Serializable;

public class CheckInventoryResponse implements Serializable {

    private String site;
    private String supplier;
    private String supplierLocation;
    private String supplierName;

    private String lineItem;
    private String manufacturer;
    private String partNumber;

    public String getSite() {
        return site;
    }
    public void setSite(String site) {
        this.site = site;
    }
    public String getSupplier() {
        return supplier;
    }
    public void setSupplier(String supplier) {
        this.supplier = supplier;
    }
    public String getSupplierLocation() {
        return supplierLocation;
    }
    public void setSupplierLocation(String supplierLocation) {
        this.supplierLocation = supplierLocation;
    }
    public String getSupplierName() {
        return supplierName;
    }
    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public String getLineItem() {
        return lineItem;
    }
    public void setLineItem(String lineItem) {
        this.lineItem = lineItem;
    }
    public String getManufacturer() {
        return manufacturer;
    }
    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }
    public String getPartNumber() {
        return partNumber;
    }
    public void setPartNumber(String partNumber) {
        this.partNumber = partNumber;
    }
}

This is my POJO class. It can be populated whenever my endpoint is being called, but I can't seem to figure out how to use the product part.

4
  • 1
    add CheckInventoryRequest class
    – Prog_G
    Commented Jun 12, 2020 at 6:05
  • @Prog_G I have the class already with those 3 values and their getters/setters.
    – A.J
    Commented Jun 12, 2020 at 6:08
  • 2
    can you share your POJO class?
    – backdoor
    Commented Jun 12, 2020 at 6:13
  • @backdoor updated
    – A.J
    Commented Jun 12, 2020 at 6:20

2 Answers 2

2

You can create a class for Products which will have the details about the product. Try the below classes:

class CheckInventoryRequest implements Serializable{
    private String supplier;
    private String supplierName;
    private String supplierLocation;
    private String site;
    private List<Product> product;

    public CheckInventoryRequest() {
    }

    public String getSupplier() {
        return supplier;
    }

    public void setSupplier(String supplier) {
        this.supplier = supplier;
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public String getSupplierLocation() {
        return supplierLocation;
    }

    public void setSupplierLocation(String supplierLocation) {
        this.supplierLocation = supplierLocation;
    }

    public String getSite() {
        return site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    public List<Product> getProduct() {
        return product;
    }

    public void setProduct(List<Product> product) {
        this.product = product;
    }

}

class Product implements Serializable{
    private Integer lineItem;
    private String manufacturer;
    private String partNumber;

    public Product() {
    }


    public Integer getLineItem() {
        return lineItem;
    }

    public void setLineItem(Integer lineItem) {
        this.lineItem = lineItem;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getPartNumber() {
        return partNumber;
    }

    public void setPartNumber(String partNumber) {
        this.partNumber = partNumber;
    }

}

Also, the json string is not valid it has extra ,. Valid json is

{
    "supplier": "foo",
    "supplierName": "ads",
    "supplierLocation": "123",
    "site": "goo",
    "product": [
        {
            "lineItem": 1,
            "manufacturer": "TEST",
            "partNumber": "123"
        },
        {
            "lineItem": 2,
            "manufacturer": "TEST 2",
            "partNumber": "3456"
        }
    ]
}
1

Put the Product as a separate class then make an item in this class as

List<Product> products = new ArrayList<Product>();

with getter and setter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.