Add streaming json item writer

Resolves BATCH-2692
This commit is contained in:
Mahmoud Ben Hassine
2018-06-20 11:06:06 +02:00
committed by Michael Minella
parent 1f900ee83b
commit 84d349ea9d
25 changed files with 2068 additions and 585 deletions

View File

@@ -1748,9 +1748,9 @@ staxItemWriter.write(trade);
----
[[jsonReadingWriting]]
=== JSON Item Readers
=== JSON Item Readers And Writers
Spring Batch provides support for reading JSON resources in the following format:
Spring Batch provides support for reading and Writing JSON resources in the following format:
[source, json]
----
@@ -1805,6 +1805,35 @@ public JsonItemReader<Trade> jsonItemReader() {
}
----
==== `JsonFileItemWriter`
The `JsonFileItemWriter` delegates the marshalling of items to the
`org.springframework.batch.item.json.JsonObjectMarshaller` interface. The contract
of this interface is to take an object and marshall it to a JSON `String`.
Two implementations are currently provided:
* link:$$https://github.com/FasterXML/jackson$$[Jackson] through the `org.springframework.batch.item.json.JacksonJsonObjectMarshaller`
* link:$$https://github.com/google/gson$$[Gson] through the `org.springframework.batch.item.json.GsonJsonObjectMarshaller`
To be able to write JSON records, the following is needed:
* `Resource`: A Spring `Resource` that represents the JSON file to write
* `JsonObjectMarshaller`: A JSON object marshaller to marshall objects to JSON format
The following example shows how to define a `JsonFileItemWriter`:
[source, java]
----
@Bean
public JsonFileItemWriter<Trade> jsonFileItemWriter() {
return new JsonFileItemWriterBuilder<Trade>()
.jsonObjectMarshaller(new JacksonJsonObjectMarshaller<>())
.resource(new ClassPathResource("trades.json"))
.name("tradeJsonFileItemWriter")
.build();
}
----
[[multiFileInput]]
=== Multi-File Input

View File

@@ -10,7 +10,7 @@ The Spring Batch 4.1 release adds the following features:
* A new `@SpringBatchTest` annotation to simplify testing batch components
* A new `@EnableBatchIntegration` annotation to simplify remote chunking configuration
* A new `JsonItemReader` to support the JSON format
* A new `JsonItemReader` and `JsonFileItemWriter` to support the JSON format
[[whatsNewTesting]]
=== `@SpringBatchTest` Annotation
@@ -155,6 +155,8 @@ APIs to read JSON objects in chunks. Spring Batch supports two libraries:
* link:$$https://github.com/google/gson$$[Gson]
To add other libraries, you can implement the `JsonObjectReader` interface.
Writing JSON data is also supported through the `JsonFileItemWriter`.
For more details about JSON support, see the
<<readersAndWriters.adoc#jsonReadingWriting,ItemReaders and ItemWriters>> chapter.