spring batch - FlatFileItemReader - token的类型为object

hwamh0ep  于 2023-03-28  发布在  Spring
关注(0)|答案(2)|浏览(70)

我第一次在我的应用程序中使用spring batch和spring data jpa。Spring batch读取一个文本文件说aCode,aName,sCode。我有一个POJO,它Map这些键,如下所示,其中一个键是另一种类型的对象

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

下面是我在BatchConfiguration类中的读取器。我如何处理这个AnotherObject。我得到一个错误说:
由于:org.springframework.beans.NotWritablePropertyException:Bean类[Pojo]的属性“sCode”无效:在输入键中找到此属性的距离〈= 5的重复匹配:[“sCode”,“aCode”,“aName”]。(考虑减小距离限制或更改输入键名称以获得更接近的匹配。)

@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;
}
mcvgt66p

mcvgt66p1#

这个例外是非常不言自明的。BeanWrapperFieldSetMapper允许对名称进行模糊匹配。您可以通过距离配置它的模糊程度。您可以在javadoc中阅读更多关于设置距离的信息:https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapper.html#setDistanceLimit-int-

pdtvr36n

pdtvr36n2#

我刚碰到这个问题。我觉得这行代码

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

应改为:

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

相关问题