0
votes

i want to write to a CSV file in spring batch writer, but i don't want to use the property names in my bean, i need a custom field name. is there another way to do it without using BeanWrapperFieldExtractor?

Here's my bean:

public class VMi {
    private String Name;
    private String OrgName;
    private String Status;
    private int CPU;
    private int RAM;
    private String IP;
}

i want to output the fields with the same names except for "OrgName" which i want it as "Organization->Name"

1
Do you mean you want different names in the header of the output file? Because the BeanWrapperFieldExtractor extracts values from fields based on getters and you can still use it in conjunction with a FlatFileHeaderCallback where you write the custom field names in the header. - Mahmoud Ben Hassine

1 Answers

0
votes

If you want to write different names in the header of the output file, you can use a FlatFileHeaderCallback. Here is a quick example:

@Bean
public FlatFileItemWriter<VMi> itemWriter() {
    return new FlatFileItemWriterBuilder<VMi>()
            .resource(new FileSystemResource("vmi.csv"))
            .name("vmiWriter")
            .delimited()
            .names("Name", "OrgName", "Status", "CPU", "RAM", "IP")
            .headerCallback(writer -> writer.write("Name, Organization->Name, Status, CPU, RAM, IP"))
            .build();
}

This will still use the BeanWrapperFieldExtractor to extract values from items based on getters, but writes a different name for OrgName in the output file.