spring beanwrapperfieldextractor flatfileitemwriter忽略缺少的空字段

jv2fixgn  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(306)

我想写入一个csv文件忽略对象字段是空的。
目前它写道:
试验,pbafff,,
对象中的第3和第4个值可以为空。
如何配置 FlatFileItemWriterBeanWrapperFieldExtractor 只会写入非 null 领域?
我的书写器配置如下:

csvWriter.setLineAggregator(new DelimitedLineAggregator<Transaction>() {
    {
        setDelimiter(",");

        setFieldExtractor(new BeanWrapperFieldExtractor<Transaction>() {
            {
                setNames(new String[] { "id", "source", "startDate", "endDate"});
            }
        });
    }
});
mepcadol

mepcadol1#

有两种方法:
实现您的自定义 LineAggregator ```
public class NullSafeDelimitedLineAggregator extends ExtractorLineAggregator {

    private String delimiter;

    public NullSafeDelimitedLineAggregator(String delimiter, FieldExtractor<T> fieldExtractor) {
        this.delimiter = delimiter;
        setFieldExtractor(fieldExtractor);
    }

    @Override
    public String doAggregate(Object[] fields) {
        return arrayToDelimitedString(fields, delimiter);
    }

    private String arrayToDelimitedString(@Nullable Object[] arr, String delim) {
        if (ObjectUtils.isEmpty(arr)) {
            return "";
        }
        if (arr.length == 1) {
            return ObjectUtils.nullSafeToString(arr[0]);
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == null || "".equals(arr[i])) {
                continue;
            }
            if (i > 0) {
                sb.append(delim);
            }
            sb.append(arr[i]);
        }
        return sb.toString();
    }
}
并按以下方式更改配置:

writer.setLineAggregator(new NullSafeDelimitedLineAggregator<>(
",",
new BeanWrapperFieldExtractor() {
{
setNames(new String[] { "id", "source", "startDate", "endDate"});
}
}));

实现您的自定义 `FieldExtractor` ```
public class NullSafeBeanWrapperFieldExtractor<T> implements FieldExtractor<T>, InitializingBean {

        private String[] names;

        public void setNames(String[] names) {
            Assert.notNull(names, "Names must be non-null");
            this.names = Arrays.asList(names).toArray(new String[names.length]);
        }

        @Override
        public Object[] extract(T item) {
            List<Object> values = new ArrayList<Object>();

            BeanWrapper bw = new BeanWrapperImpl(item);
            for (String propertyName : this.names) {
                Object value = bw.getPropertyValue(propertyName);
                if(value == null || "".equals(value)) {
                    continue;
                }
                values.add(value);
            }
            return values.toArray();
        }

        @Override
        public void afterPropertiesSet() {
            Assert.notNull(names, "The 'names' property must be set.");
        }

    }

并按以下方式更改配置:

writer.setLineAggregator(new DelimitedLineAggregator<Transaction>() {
            {
                setDelimiter(",");
                setFieldExtractor(new NullSafeBeanWrapperFieldExtractor<Transaction>() {
                    {
                        setNames(new String[] { "id", "source", "startDate", "endDate"});
                    }
                });
            }
        });

要点:您应该根据一个问题从两个选项中选择一个:知道数组中有多少值对您来说重要吗?如果是- LineAggregator ,否- FieldExtractor .

相关问题