包阅导读总结
1.
关键词:Spring Batch、JSON、Item Reader、Item Writer、Java
2.
总结:本文介绍了在Spring Batch中使用JsonItemReader和JsonFileItemWriter类读写JSON数据的方法。包括项目设置、创建示例文件和模型类,详细阐述了Item Reader和Writer的配置及示例代码,最后给出完整示例和结论。
3.
主要内容:
– 项目设置
– 创建Maven项目,配置pom.xml添加依赖
– 创建示例JSON文件data.json
– 创建用于表示JSON数据的User模型类
– JSON Item Reader示例
– 创建JsonItemReaderConfig类,使用@StepScope创建JsonItemReader bean来读取JSON文件中的User对象
– JSON File Writer示例
– 创建JsonItemWriterConfig类,创建JsonFileItemWriter bean将User对象写入output.json文件
– 完整示例代码
– SpringbatchjsonApplication类中设置Job和Step,包括读取、处理和写入的流程,其中处理器打印用户邮箱和姓名
– 运行应用会读取data.json,处理数据并写入output.json,在控制台输出处理结果
– 结论
– 探讨了在Spring Boot项目中用Spring Batch和Jackson实现JSON读写,学习了配置及动态步骤配置
思维导图:
文章地址:https://www.javacodegeeks.com/implementing-json-item-reader-and-writer-in-spring-batch.html
文章来源:javacodegeeks.com
作者:Omozegie Aziegbe
发布时间:2024/7/16 12:55
语言:英文
总字数:864字
预计阅读时间:4分钟
评分:82分
标签:Spring 批处理,JSON 处理,Java,批处理应用程序,Spring Boot
以下为原文内容
本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com
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.
@Configurationpublic 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 theItemReader
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 thedata.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.
@Configurationpublic 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.
@SpringBootApplicationpublic 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.