Spring-batch ItemProcessor data in the form of a list to model

483 views Asked by At

I'm using a custom itemReader to read data from an external rest API, and it's working great. However, the problem arises when processing the data with ItemProcessor into my model class. Unfortunately, the API response is an object with an array nested inside of it, which means I have to use a list referencing another class to store the data inside of it.

picture of API response

StockDTO data class:

public class StockDTO {

    //  We will change the field data types later when we process the data into our model.
    private String from;
    private String to;
    private List<ProductDTO> products;

    //  Getters and Setters allow RestTemplate to set the data from the external rest API.
    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

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

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

ProductDTO data class (list data):

public class ProductDTO {

    //  We will change the field data types later when we process the data into our model.
    private String sku;
    private String name;
    private String startDate;

    //  Getters and Setters allow RestTemplate to set the data from the external rest API.
    public String getSku() {
        return sku;
    }

    public void setSku(String sku) {
        this.sku = sku;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }
}

model class:

@Entity
public class Stock {

    @Id
    private long id;
    private int item_from;
    private int item_to;
    private long sku;
    private String name;

    public long getId() {
        return id;
    }

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

    public int getItem_from() {
        return item_from;
    }

    public void setItem_from(int item_from) {
        this.item_from = item_from;
    }

    public int getItem_to() {
        return item_to;
    }

    public void setItem_to(int item_to) {
        this.item_to = item_to;
    }

    public long getSku() {
        return sku;
    }

    public void setSku(long sku) {
        this.sku = sku;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

ItemProcessor<StockDTO, Stock>

public class StockDataProcessor implements ItemProcessor<StockDTO, Stock> {

    @Override
    public Stock process(StockDTO stockDTO) throws Exception {

        Stock stock = new Stock();

        stock.setItem_from(Integer.parseInt(stockDTO.getFrom()));
        stock.setItem_to(Integer.parseInt(stockDTO.getTo()));
        //  I'm only able to get the first index sku, name from the list:
        stock.setSku(Long.parseLong(stockDTO.getProducts().get(0).getSku()));
        stock.setName(stockDTO.getProducts().get(0).getName());

        return stock;
    }
}

Should I create another model class called Product so that I can create ItemProcessor<ProductDTO, Product> or can I get all the list items from ItemProcessor<StockDTO, Stock> without creating another processor? Thank you.

1

There are 1 answers

3
httPants On

You could create another model class called Product and modify the Stock class to include a List and modify your ItemProcessor to populate the list of Product in the Stock class.

Alternatively, you could make your ItemProcessor return a List instead of just a single Stock. Your ItemWriter would then need to process a List<List>.