BeanItemContainer equivalent in vaadin 14

I am upgrading an old system from an earlier version of vaadin to vaadin 14. In the previous version, I used BeanItemContainer with Beans to populate a grid and export reports to Jasper Reports based on the filtered input.

What is the current replacement for BeanItemContainer in the current version?

BeanItemContainer<SampleBean> sample_container=new BeanItemContainer<SampleBean>(SampleBean.class);
	for(int i=0; i<rows; i++)
		{
						 SampleBean data=new SampleBean();
							data.setName("Allan") //bean fields setters and getters
							data.setEmail("allan@vaadin.com") //bean fields setters and getters
	    }
						 sample_container.add(data) //add the bean to the container
						 
						 //then set the container as the datasource for the grid

You can use a java.util.ArrayList<SampleBean>.

    List<SampleBean> sample_container=new ArrayList<SampleBean>();
	for(int i=0; i<rows; i++)
		{
						 SampleBean data=new SampleBean();
							data.setName("Allan") //bean fields setters and getters
							data.setEmail("allan@vaadin.com") //bean fields setters and getters
						 sample_container.add(data) //add the bean to the container
		}

    Grid<SampleBean> grid = new Grid<>(SampleBean.class);
    grid.setItems(sample_container);

Example:

final List<Customer> lister = new ArrayList<>();
	grid3.setColumns("safdc", "as", "fas", "asdf",
                   "asf", "awef", "asf", "aasdf");
	grid3.setItems(lister);

This resulting in invalid json error on Vaadin!
Grid or table cannot add more than 10 rows!

Any solution for this?

If you’re calling grid.setColumns with String parameters,
a) You need to use the Grid constructor that uses a Bean class parameter. In your case, it must be constructed with Grid<Customer> grid3 = new Grid<>(Customer.class) instead of new Grid<>().
b) The parameter values need to match the Property names in your Bean class. In your case Customer should have methods like getSafdc(), getAs(), getFas() and so on.

Olli tietavainen Thank you for the reply.

public static final Grid<Customer> grid3 = new Grid<>(Customer.class);
	
	final List<Customer> lister = new ArrayList<>();
	grid3.setColumns("safdc", "as", "fas", "asdf",
                   "asf", "awef", "asf", "aasdf");
	grid3.setItems(lister);
	
	getSafdc(), getAs(), getFas()

It was the same as you said… i get the same error INVALID JSON and some random repeated code of grid items.

You should reduce that to a minimal reproducible example - it’s not easy to say anything about that code as it is.

That’s not what I meant by a minimal reproducible example. StackOverflow has some guidelines here: https://stackoverflow.com/help/minimal-reproducible-example
This page is also a good read: http://sscce.org/

Your example code contains things that are not necessary (anything related to REST access, authentication, etc.) and it’s missing important relevant pieces (the Customer class).

import javax.validation.constraints.*;
public class Customer implements Cloneable{

	    private String parmCompanyID;
	    
	    private String parmName;
	    
	    private String parmAddress;
	    
	    private String parmCustomerAccount;
	    
	    private String parmEmail;
	    
	    private String parmLanguage;
   
	    public Customer() {}

	    public Customer(String parmCompanyID, String parmName, String parmAddress,
	    		String parmCustomerAccount, String parmEmail, String parmLanguage) {
      super();
    
      this.parmCompanyID = parmCompanyID;
      this.parmName = parmName;
      this.parmAddress = parmAddress;
      this.parmCustomerAccount = parmCustomerAccount;
      this.parmEmail = parmEmail;
      this.parmLanguage = parmLanguage;
  }

	    public String getParmCompanyID() {
	        return parmCompanyID;
	    }

	    public void setParmCompanyID(String parmCompanyID) {
	        this.parmCompanyID = parmCompanyID;
	    }

	    public String getParmName() {
	        return parmName;
	    }

	    public void setParmName(String parmName) {
	        this.parmName = parmName;
	    }
	    
	    public String getParmAddress() {
	        return parmAddress;
	    }

	    public void setParmAddress(String parmAddress) {
	        this.parmAddress = parmAddress;
	    }
	    
	    public String getParmCustomerAccount() {
	        return parmCustomerAccount;
	    }

	    public void setParmCustomerAccount(String parmCustomerAccount) {
	        this.parmCustomerAccount = parmCustomerAccount;
	    }
	    
	  
	    public String getParmEmail() {
	        return parmEmail;
	    }
	
	    public void setParmEmail(String parmEmail) {
	        this.parmEmail = parmEmail;
	    }
	    
	    public String getParmLanguage() {
	        return parmLanguage;
	    }

	    public void setParmLanguage(String parmLanguage) {
	        this.parmLanguage = parmLanguage;
	    }	  
}
public class CustomerAccount extends VerticalLayout {
	
		final List<Customer> lister = new ArrayList<>();
		public static final Grid<Customer> grid3 = new Grid<>(Customer.class);
		 
    public CustomerAccount() {   
       getRes("**some string**");   
	        grid3.setColumns("parmCustomerAccount", "parmName", "parmAddress", "parmCompanyID",
                   "parmEmail", "parmLanguage", "parmCustomerAccount1", "emailid1");  
	        add(grid3);
  }
    public void getRes(String cid)   
	{
    	 HibernateUtil.truncate();
         String wsURL = "http://aedfwfwfwefsome link .svc?wsdl";        
         URL url = null;
         URLConnection connection = null;
         HttpURLConnection httpConn = null;
         String responseString = null;
         String outputString="";
         OutputStream out = null;
         InputStreamReader isr = null;
         BufferedReader in = null;
         String user = "***";
 		String password = "*******";
 		String authString = user + ":" + password;
 		System.out.println("auth string: " + authString);
 		byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
 		String authStringEnc = new String(authEncBytes);
 		System.out.println("Base64 encoded auth string: " + authStringEnc);	    
		
         String xmlInput =";yukjnfxergedgdbdb some xml 
                  </soapenv:Body> </soapenv:Envelope>aedhbrfthrh";
         try
         {	
         	if(wsURL.contains(" "))
         		wsURL = wsURL.replace(" ", "%20");
             url = new URL(wsURL);
             connection = url.openConnection();
             httpConn = (HttpURLConnection) connection;
             httpConn.setRequestProperty("Authorization", "Basic " + authStringEnc);
  
             byte[] buffer = new byte[xmlInput.length()]
;
             buffer = xmlInput.getBytes();
  
             String SOAPAction = "http://tempuri.org/GEICustomerService/getCustomer";
          
             httpConn.setRequestProperty("Content-Length", String
                      .valueOf(buffer.length));
             
             httpConn.setRequestProperty("Content-Type",
                     "text/xml; charset=utf-8");
                           
             httpConn.setRequestProperty("SOAPAction", SOAPAction);
             
             httpConn.setRequestMethod("POST");
             
             httpConn.setDoOutput(true);
             
             httpConn.setDoInput(true);
             
             out = httpConn.getOutputStream();
             out.write(buffer);
             out.close(); 
              
             // Read the customer details response
             isr = new InputStreamReader(httpConn.getInputStream());
             in = new BufferedReader(isr);
              
             while ((responseString = in.readLine()) != null) 
             {
                 outputString = outputString + responseString;
             }
          
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
              InputSource is = new InputSource(new StringReader(outputString));
              
              Document document = db.parse(is);
			  
           NodeList nodeLst = document.getElementsByTagName("b:GEICustomerDataContract");
           NodeList parmAddress1 = document.getElementsByTagName("b:parmAddress");
           NodeList parmAddress2 = document.getElementsByTagName("b:parmCompanyID");
           NodeList parmAddress3 = document.getElementsByTagName("b:parmCustomerAccount");
           NodeList parmAddress4 = document.getElementsByTagName("b:parmEmail");
           NodeList parmAddress5 = document.getElementsByTagName("b:parmLanguage");
           NodeList parmAddress6 = document.getElementsByTagName("b:parmName");
  
         	for (nodenum1 = 0; nodenum1 < 20; nodenum1++){
                     	  
           String webServiceResponse = nodeLst.item(nodenum1).getTextContent();
           String webServiceResponse1 = parmAddress3.item(nodenum1).getTextContent();
           String webServiceResponse2 = parmAddress6.item(nodenum1).getTextContent();
           String webServiceResponse3 = parmAddress1.item(nodenum1).getTextContent();
           String webServiceResponse4 = parmAddress2.item(nodenum1).getTextContent();
           String webServiceResponse5 = parmAddress4.item(nodenum1).getTextContent();
           String webServiceResponse6 = parmAddress5.item(nodenum1).getTextContent();                          
           
          lister.add(new Customer(webServiceResponse4, webServiceResponse2, webServiceResponse3, 
        		  webServiceResponse1, webServiceResponse5, webServiceResponse6));   

         }
         System.out.println(l1);  
       
      grid1.setItems(lister);
         
          } 
         catch (Exception e)         
         {
             e.printStackTrace();
         }
    }
}

for (nodenum1 = 0; nodenum1 < 10; nodenum1++) It WORKS fine for 10 rows in grid and if it is for (nodenum1 = 0; nodenum1 < 20; nodenum1++) doesnt work and shows invalid json!!

I’m sorry to be so pedantic about this, but that’s still not a reproducible code example (and it’s not minimal, either).