jsonspringspring-batchitemwriter

How to write the output from DB to a JSON file using Spring batch?


I am new to spring batch and there is a requirement for me to read the data from DB and write in to JSON format. whats the best way to do this ? Any api's are there? or we need to write custom writer ? or i need to user JSON libraries such as GSON or JACKSON ? Please guide me...


Solution

  • To read data from a relational database, you can use one of the database readers. You can find an example in the spring-batch-samples repository.

    To write JSON data, Spring Batch 4.1.0.RC1 provides the JsonFileItemWriter that allows you to write JSON data to a file. It collaborates with a JsonObjectMarshaller to marshal an object to JSON format. Spring Batch provides support for both Gson and Jackson libraries (you need to have the one you want to use in the classpath). You can find more details here.

    Hope this helps.