Spring Integrating CSV file into POJO transformer

I am having some problems getting values ​​from the payload in jdbc-outbound-adapter. See below for details.

a) Download csv files from remote server using int-sftp: inbound adapter-pipe

b). Adding filename to header with header-enricher

<int:transformer id="filetoPojoTransformer" input-channel="outputChannel" method="processContent"
          output-channel="fileOutputChannel" ref="FileToPOJOTransformer" />

      

from. Convert file content to POJO using custom transformer (used openCSV library for this)

<int:transformer id="filetoPojoTransformer" input-channel="outputChannel" method="processContent"
          output-channel="fileOutputChannel" ref="FileToPOJOTransformer" />

      

d) FileToPOJOTransformer class below

public class FileToPOJOTransformer { 

    public List processContent(File file){   
            String[] columns = null;
            List<String> lines = null;
            List<Model1> list = null;

                try {
                    lines = Files.readAllLines(file.toPath(), 
                            StandardCharsets.UTF_8);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                columns = lines.get(0).split(",");              

                final ColumnPositionMappingStrategy<Model1> strategy = new ColumnPositionMappingStrategy<>();
                strategy.setType(Model1.class);
                strategy.setColumnMapping(columns);
                final CsvToBean<Model1> csvToBean = new CsvToBean<>();

                try (final Reader reader = new FileReader(file)) {
                    list = csvToBean.parse(strategy, reader);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
        return list;
        }

}

      

e) Using file name-based router delegation to appropriate channels

<int:recipient-list-router id="customRouter" input-channel="fileOutputChannel" default-output-channel="nullChannel">
       <int:recipient channel="channel1" selector-expression="headers['fileName'].startsWith('File1')"/>
       <int:recipient channel="channel2" selector-expression="headers['fileName'].startsWith('File2')"/>       
     </int:recipient-list-router>

      

f) Now I need to save the CSV content to the database, so I used jdbc-outbound-adapter

<int-jdbc:outbound-channel-adapter  
        id="jdbcOutBoundAdapterEndpoint1" channel="channel1"  
        query="insert into ImodiumTracking.ResponsesSent (Col1) 
        values(:payload.get(0).Col1)"
        data-source="dataSource">  
    </int-jdbc:outbound-channel-adapter> 

      

I tried different options for getting bean values ​​but didn't work. How to get bean values ​​in jdbc outbound adapter from payload that contains POJO list

+3


source to share


1 answer


  • Show expcetion
  • Show the contents of your List

  • Why not split this one List

    by storing each item in the DB?

Try the following:



query="insert into ImodiumTracking.ResponsesSent (Col1) values(:payload[0].col1)"

      

if your POJO has getCol1()

0


source







All Articles