user700
user700

Reputation: 111

spring batch - FlatFileItemReader - token is of type object

I am using spring batch and spring data jpa in my application first time. Spring batch reads a text file says aCode,aName,sCode. I have a POJO which maps these keys like below, one the the keys is of another type of object

 public class Pojo{
  private AnotherObject sCode;  
  private String aName;
  private String aCode;
 }

Below is my reader in BatchConfiguration class. How do I handle this AnotherObject. I get an error saying:

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'sCode' of bean class [Pojo]: Duplicate match with distance <= 5 found for this property in input keys: ["sCode","aCode","aName"]. (Consider reducing the distance limit or changing the input key names to get a closer match.)

 @Bean
  @StepScope
  public FlatFileItemReader<Area> reader(
      @Value("#{stepExecutionContext[fromId]}") final String fromId,
      @Value("#{stepExecutionContext[toId]}") final String toId,
      @Value("#{stepExecutionContext[name]}") final String name) {
    FlatFileItemReader<Area> reader = new FlatFileItemReader<>();
    try{

      reader.setResource(new ClassPathResource("area_temp.txt"));   
      LineMapper<Area> areaMapper = createAreaLineMapper();
      reader.setLineMapper(areaMapper);
    }
    catch(Exception e){
      e.printStackTrace();
    }
    return reader;
  }

  private LineMapper<Area> createAreaLineMapper() {
    DefaultLineMapper<Area> areaLineMapper = new DefaultLineMapper<>();

    LineTokenizer areaTokenizer = createAreaTokenizer();
    areaLineMapper.setLineTokenizer(areaTokenizer);

    FieldSetMapper<Area> areaInformationMapper = createAreaInformationMapper();
    areaLineMapper.setFieldSetMapper(areaInformationMapper);

    return areaLineMapper;
}

 private LineTokenizer createAreaTokenizer() {
    DelimitedLineTokenizer areaLineTokenizer = new DelimitedLineTokenizer();
    areaLineTokenizer.setDelimiter("|");
    areaLineTokenizer.setNames(new String[]{"sCode","aCode","aName"});
    return areaLineTokenizer;
}
 private FieldSetMapper<Area> createAreaInformationMapper() {
    BeanWrapperFieldSetMapper<Area> areaMapper = new BeanWrapperFieldSetMapper<>();
    areaMapper.setTargetType(Area.class);
    return areaMapper;
}

Upvotes: 1

Views: 5438

Answers (2)

Nina
Nina

Reputation: 11

I just came across this issue. I think this line of code

areaLineTokenizer.setNames(new String[]{"sCode","aCode","aName"});

should be changed to:

areaLineTokenizer.setNames(new String[]{"sCode","aName","aCode"});

Upvotes: 0

Michael Minella
Michael Minella

Reputation: 21503

The exception is pretty self explanatory. BeanWrapperFieldSetMapper allows for fuzzy matching on the names. You can configure how fuzzy it is via the distance. You can read more about setting that distance in the javadoc here: https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapper.html#setDistanceLimit-int-

Upvotes: 1

Related Questions