spring-batch read in null int values

青春壹個敷衍的年華 提交于 2020-01-17 01:29:09

问题


I have a file reader to read a customer csv file If the int fields are empty i get error Field error in object 'target' on field 'custPaymentTerm': rejected value []; codes [typeMismatch.target.custPaymentTerm,typeMismatch.custPaymentTerm,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.custPaymentTerm,custPaymentTerm]; arguments []; default message [custPaymentTerm]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'custPaymentTerm'; nested exception is java.lang.NumberFormatException: For input string: ""]

The class to read the file. How does spring-batch-job read , handle null int values.

The second line of my csv data file throws the null error !

public static void readCustomers(String pathToFile) throws Exception {  
     System.out.println(" THE path is:"+pathToFile);
     FlatFileItemReader<Customer> reader = new FlatFileItemReader<>();
     reader.setResource(new FileSystemResource(pathToFile));


     DelimitedLineTokenizer delimitedTokeniser = new DelimitedLineTokenizer();
     delimitedTokeniser.setNames(new String[]{"valueA", "custPaymentTerm","valueC"});
     DefaultLineMapper<Customer> lineMapper = new DefaultLineMapper<Customer>();
     lineMapper.setLineTokenizer(delimitedTokeniser);
     lineMapper.setFieldSetMapper(new BeanWrapperFieldSetMapper<Customer>() {{setTargetType(Customer.class);}});
     reader.setLineMapper(lineMapper);
     reader.setLinesToSkip(1); //do not read the first line of the csv file.
     reader.open(new ExecutionContext());
     Customer store = null;
     List<Customer> stores = new ArrayList<>();
     do {
         store = reader.read();
         if (store != null) {
             System.out.println("######## ------->"+store.getCustPaymentTerm());
             stores.add(store);
         }
     } while (store != null);
}

public Customer(){
    String valueA;
    int custPaymentTerm;
    String valueC;
}

Data file content

apple,1,cat
dog,,rat

回答1:


In stead of using the Spring provided BeanWrapperFieldSetMapper, you should write your own implementation of the FieldSetMapper and add that to your linemapper.

 lineMapper.setFieldSetMapper(new MyCustomFieldSetMapper());

Within the mapFieldSet(...) method in that class you could create a new Customer with all the mapped field. That would probably look something like this:

Customer mapFieldSet(FieldSet fs) {
    Customer customer = new Customer();
    customer.setCustPaymentTerm(fs.readInt("custPaymentTerm", 0);
    // set your other fields here
    return customer;
}

The zero in the 'readInt(..., 0)' call is the default value in case of a blank value is read from the line.



来源:https://stackoverflow.com/questions/50020445/spring-batch-read-in-null-int-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!