From 80d0015bb7137d988d09891058f3ee4b16008094 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Tue, 11 Sep 2018 21:05:28 +0200 Subject: [PATCH] Update docs with new features of v4.1.0.M3 --- spring-batch-docs/asciidoc/appendix.adoc | 5 +- .../asciidoc/readersAndWriters.adoc | 38 +++++++++++ spring-batch-docs/asciidoc/whatsnew.adoc | 66 +++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/spring-batch-docs/asciidoc/appendix.adoc b/spring-batch-docs/asciidoc/appendix.adoc index 5b4a78cc2..824b7b55c 100644 --- a/spring-batch-docs/asciidoc/appendix.adoc +++ b/spring-batch-docs/asciidoc/appendix.adoc @@ -58,7 +58,8 @@ Spring Data repository implementation. |StoredProcedureItemReader|Reads from a database cursor resulting from the execution of a database stored procedure. See link:readersAndWriters.html#StoredProcedureItemReader[`StoredProcedureItemReader`] -|StaxEventItemReader|Reads via StAX. see link:readersAndWriters.html#flatFileItemReader[`FlatFileItemReader`]. +|StaxEventItemReader|Reads via StAX. see link:readersAndWriters.html#StaxEventItemReader[`StaxEventItemReader`]. +|JsonItemReader|Reads items from a Json document. see link:readersAndWriters.html#JsonItemReader[`JsonItemReader`]. |=============== @@ -119,5 +120,7 @@ |StaxEventItemWriter|Uses a `Marshaller` implementation to convert each item to XML and then writes it to an XML file using StAX. +|JsonFileItemWriter|Uses a `JsonObjectMarshaller` implementation to + convert each item to Json and then writes it to an Json file. |=============== diff --git a/spring-batch-docs/asciidoc/readersAndWriters.adoc b/spring-batch-docs/asciidoc/readersAndWriters.adoc index 1dd3ecd31..1cfd86b76 100644 --- a/spring-batch-docs/asciidoc/readersAndWriters.adoc +++ b/spring-batch-docs/asciidoc/readersAndWriters.adoc @@ -1348,6 +1348,25 @@ In the previous example, the `BeanWrapperFieldExtractor` described earlier in th chapter is used to turn the name and credit fields within `CustomerCredit` into an object array, which is then written out with commas between each field. +It is also possible to use the `FlatFileItemWriterBuilder.DelimitedBuilder` to +automatically create the `BeanWrapperFieldExtractor` and `DelimitedLineAggregator` +as shown in the following example: + +.Java Configuration +[source, java, role="javaContent"] +---- +@Bean +public FlatFileItemWriter itemWriter(Resource outputResource) throws Exception { + return new FlatFileItemWriterBuilder() + .name("customerCreditWriter") + .resource(outputResource) + .delimited() + .delimiter(",") + .names(new String[] {"name", "credit"}) + .build(); +} +---- + [[fixedWidthFileWritingExample]] ===== Fixed Width File Writing Example @@ -1418,6 +1437,25 @@ The underlying implementation is built using the same language. Most details on how to configure a formatter can be found in the Javadoc of link:$$https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html$$[Formatter]. +It is also possible to use the `FlatFileItemWriterBuilder.FormattedBuilder` to +automatically create the `BeanWrapperFieldExtractor` and `FormatterLineAggregator` +as shown in following example: + +.Java Configuration +[source, java, role="javaContent"] +---- +@Bean +public FlatFileItemWriter itemWriter(Resource outputResource) throws Exception { + return new FlatFileItemWriterBuilder() + .name("customerCreditWriter") + .resource(outputResource) + .formatted() + .format("%-9s%-2.0f") + .names(new String[] {"name", "credit"}) + .build(); +} +---- + [[handlingFileCreation]] ===== Handling File Creation diff --git a/spring-batch-docs/asciidoc/whatsnew.adoc b/spring-batch-docs/asciidoc/whatsnew.adoc index 191b1ed71..c1f84a97b 100644 --- a/spring-batch-docs/asciidoc/whatsnew.adoc +++ b/spring-batch-docs/asciidoc/whatsnew.adoc @@ -254,3 +254,69 @@ public BeanValidatingItemProcessor beanValidatingItemProcessor() throws } ---- +=== JSR-305 support + +This release adds support for JSR-305 annotations. It leverages Spring Framework’s +link:$$https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#null-safety$$[Null-safety] +annotations and adds them on all public APIs of Spring Batch. + +These annotations will not only enforce null-safety when using Spring Batch APIs, +but also can be used by IDEs to provide useful information related to nullability. +For example, if a user wants to implement the `ItemReader` interface, any IDE +supporting JSR-305 annotations will generate something like: + +[source, java] +---- +public class MyItemReader implements ItemReader { + + @Nullable + public String read() throws Exception { + return null; + } + +} +---- + +The `@Nullable` annotation present on the `read` method makes it clear that the +contract of this method says it may return `null`. This enforces what is said in its +Javadoc, that the `read` method should return `null` when the data source is exhausted. + +=== `FlatFileItemWriterBuilder` enhancements + +Another small feature added in this release is a simplification of the configuration +for the writing of a flat file. Specifically, these updates simplify the configuration +of both a delimited and fixed width file. Below is an example of before and after the change. + +[source, java] +---- +// Before +@Bean +public FlatFileItemWriter itemWriter(Resource resource) { + BeanWrapperFieldExtractor fieldExtractor = + new BeanWrapperFieldExtractor(); + fieldExtractor.setNames(new String[] {"field1", "field2", "field3"}); + fieldExtractor.afterPropertiesSet(); + + DelimitedLineAggregator aggregator = new DelimitedLineAggregator(); + aggregator.setFieldExtractor(fieldExtractor); + aggregator.setDelimiter(";"); + + return new FlatFileItemWriterBuilder() + .name("itemWriter") + .resource(resource) + .lineAggregator(aggregator) + .build(); +} + +// After +@Bean +public FlatFileItemWriter itemWriter(Resource resource) { + return new FlatFileItemWriterBuilder() + .name("itemWriter") + .resource(resource) + .delimited() + .delimiter(";") + .names(new String[] {"field1", "field2", "field3"}) + .build(); +} +----