VOOZH about

URL: https://www.javacodegeeks.com/implementing-json-item-reader-and-writer-in-spring-batch.html

⇱ Implementing JSON Item Reader and Writer in Spring Batch - Java Code Geeks


Spring Batch is a popular framework for building batch applications in Java. One of the key features of Spring Batch is its ability to read and write data in various formats, including JSON. In this article, we will explore how to use Spring Batch to read and write JSON data using the JsonItemReader and JsonFileItemWriter classes.

1. Project Setup

Let’s begin by creating a Maven project. Below is the pom.xml file with all the required dependencies included.

 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-batch</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-databind</artifactId>
 </dependency>
 </dependencies>

1.1 JSON Example File

Let’s create an example JSON file data.json that contains a list of users.

[
 {
 "id": 1,
 "name": "John Franklin",
 "email": "john.franklin@jcg.com"
 },
 {
 "id": 2,
 "name": "Thomas Smith",
 "email": "thomas.smith@jcg.com"
 },
 {
 "id": 3,
 "name": "Adams Jefferson",
 "email": "adams.jefferson@jcg.com"
 }
]

1.2 Model Class

Create a User class to represent the data in the JSON file.

public class User {
 
 private int id;
 private String name;
 private String email;

 public int getId() {
 return id;
 }

 public void setId(int id) {
 this.id = id;
 }

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public String getEmail() {
 return email;
 }

 public void setEmail(String email) {
 this.email = email;
 } 
}

2. JSON Item Reader Example

To read the JSON data using Spring Batch, we need to create a JsonItemReader bean and configure it to read the JSON file.

In our example, we will use the JsonItemReaderBuilder to create an ItemReader that reads User objects from our JSON file (data.json). We will also use the @StepScope annotation to ensure that the reader is instantiated within the scope of a step, allowing for dynamic step-based configurations.

@Configuration
public class JsonItemReaderConfig {

 @Bean
 @StepScope
 public JsonItemReader<User> jsonItemReader() {
 ObjectMapper objectMapper = new ObjectMapper();

 return new JsonItemReaderBuilder<User>()
 .jsonObjectReader(new JacksonJsonObjectReader<>(User.class))
 .resource(new ClassPathResource("data.json"))
 .name("jsonItemReader")
 .build();
 }
}

In the code above:

  • The @StepScope annotation ensures that the ItemReader bean is instantiated and managed within the scope of a step. This allows for dynamic parameters or configurations to be injected into the reader.
  • JsonItemReaderBuilder: Configures the reader to read User objects from the data.json file using Jackson for JSON parsing.

3. JSON File Writer Example

The ItemWriter is responsible for writing the processed data to a JSON file. We will use the JsonItemWriterBuilder to create an ItemWriter that writes User objects to a file named output.json. Unlike the ItemReader, the ItemWriter does not require the @StepScope annotation, as it does not need dynamic configurations.

@Configuration
public class JsonItemWriterConfig {
 
 @Bean
 public JsonFileItemWriter<User> jsonItemWriter() {
 return new JsonFileItemWriterBuilder<User>()
 .jsonObjectMarshaller(new JacksonJsonObjectMarshaller<>())
 .resource(new FileSystemResource("output.json"))
 .name("jsonItemWriter")
 .build();
 }
}

This block of code defines a ItemWriter bean named jsonItemWriter for writing User objects to a JSON file. It uses the JsonFileItemWriterBuilder to create the writer, specifying a JacksonJsonObjectMarshaller to convert User objects to JSON format. The output file is set to output.json located in the file system. The writer is configured with the name jsonItemWriter and will handle the serialization of User objects to the specified JSON file.

4. Full Example Code

Below is a full example code where we set up the Spring Batch configuration to read from a JSON file and write to another JSON file.

@SpringBootApplication
public class SpringbatchjsonApplication {

 @Bean
 Job job(Step jsonstep, JobRepository jobRepository) {

 var builder = new JobBuilder("json-job", jobRepository);
 return builder
 .start(jsonstep)
 .build();
 }

 @Bean
 public Step jsonstep(JsonItemReader<User> reader,
 JsonFileItemWriter<User> writer,
 JobRepository jobRepository,
 PlatformTransactionManager transactionManager) {

 var builder = new StepBuilder("json-step", jobRepository);
 return builder
 .<User, User>chunk(1, transactionManager)
 .reader(reader)
 .processor(jsonItemProcessor())
 .writer(writer)
 .build();
 }

 @Bean
 @StepScope
 public JsonItemReader<User> jsonItemReader() {
 ObjectMapper objectMapper = new ObjectMapper();

 return new JsonItemReaderBuilder<User>()
 .jsonObjectReader(new JacksonJsonObjectReader<>(User.class))
 .resource(new ClassPathResource("data.json"))
 .name("jsonItemReader")
 .build();
 }

 @Bean
 public ItemProcessor<User, User> jsonItemProcessor() {
 return user -> {
 // Process user if needed
 System.out.println("" + user.getEmail() + " " + user.getName() );
 return user;
 };
 }

 @Bean
 public JsonFileItemWriter<User> jsonItemWriter() {
 return new JsonFileItemWriterBuilder<User>()
 .jsonObjectMarshaller(new JacksonJsonObjectMarshaller<>())
 .resource(new FileSystemResource("output.json"))
 .name("jsonItemWriter")
 .build();
 }

 public static void main(String[] args) {
 SpringApplication.run(SpringbatchjsonApplication.class, args);
 }

}

In the above code snippet:

The job bean configures a job named json-job that starts with a single step, jsonstep. The jsonstep bean defines the step logic, where each chunk processes one item at a time (chunk(1)). It uses a JsonItemReader to read User objects from a data.json file, an ItemProcessor to process each User object, and a JsonFileItemWriter to write the processed User objects to an output.json file.

The jsonItemProcessor bean processes each User object, implemented to print the user’s email and name to the console.

With this setup, running the application will read the data.json file, process the data, and write the output to output.json in the specified directory.

Output on the console:

5. Conclusion

In this article, we explored how to implement JSON item readers and writers using Spring Batch and Jackson in a Spring Boot project. We learned how to configure separate files for JsonItemReader and JsonFileItemWriter, leveraging @StepScope for dynamic step-based configuration.

6. Download the Source Code

This was an article showcasing a JSON reader and writer example.

Download
You can download the full source code of this example here: JSON Reader Writer example
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Omozegie Aziegbe
Omozegie Aziegbe
July 16th, 2024Last Updated: July 16th, 2024
0 1,930 3 minutes read

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz