
本教程旨在解决spring boot应用中从`src/main/resources`目录读取不断更新的json文件时遇到的问题。我们将深入分析`getresourceasstream`的局限性,并提供一种健壮的解决方案,通过配置外部文件路径并结合spring的`resourceloader`,实现周期性地从文件系统读取最新json数据,并将其同步至数据库,确保数据实时性与应用稳定性。
在Spring Boot应用开发中,有时我们需要周期性地读取并处理一个不断更新的外部JSON文件,并将其数据持久化到数据库。然而,直接使用Class.getResourceAsStream()方法从src/main/resources目录读取文件时,可能会遇到文件内容无法实时更新的问题。本指南将详细解释这一现象的原因,并提供一个基于Spring ResourceLoader的专业解决方案。
当您将JSON文件放置在Spring Boot项目的src/main/resources目录下时,该文件在项目构建(如打包成JAR或WAR)后,会被视为应用程序的“classpath资源”。
因此,如果您的JSON文件是需要被外部进程或应用自身周期性更新的,并期望应用能读取到这些更新,那么将其作为classpath资源处理是不可行的。我们需要一种直接访问文件系统的方式。
为了解决上述问题,我们需要让Spring Boot应用直接从文件系统而不是classpath中读取文件。Spring框架提供了强大的ResourceLoader接口,能够以统一的方式加载各种类型的资源,包括文件系统资源、classpath资源、URL资源等。
核心思路如下:
我们将通过一个具体的示例来演示如何实现这一功能。
在src/main/resources/application.properties或application.yml中定义JSON文件的路径。为了在开发环境中方便测试,我们可以将其指向项目内部的src/main/resources目录,但在生产环境中,建议指向一个外部的、独立的目录。
src/main/resources/application.properties
# 定义JSON文件的文件系统路径 # 在开发环境中,可以指向项目内部的resources目录 # 在生产环境中,应指向外部的绝对路径,例如:file:/opt/app/data/file.json app.data.json-file-path=file:./src/main/resources/json/file.json
这里使用file:前缀明确指示ResourceLoader这是一个文件系统资源。./表示当前工作目录。
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
首先,确保您的数据模型(Master)和JPA仓库(MasterRepository)已正确定义。
src/main/java/com/example/demo/model/Master.java
package com.example.demo.model;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Master {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String value;
// 根据您的JSON结构添加其他字段
}src/main/java/com/example/demo/Repository/MasterRepository.java
package com.example.demo.Repository;
import com.example.demo.model.Master;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MasterRepository extends CrudRepository<Master, Long> {
}MasterService负责处理Master对象的业务逻辑,特别是保存操作。
src/main/java/com/example/demo/Services/MasterService.java
package com.example.demo.Services;
import com.example.demo.Repository.MasterRepository;
import com.example.demo.model.Master;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; // 导入Transactional
import java.util.List;
@Service
public class MasterService {
private final MasterRepository masterRepository;
// 推荐使用构造器注入
public MasterService(MasterRepository masterRepository) {
this.masterRepository = masterRepository;
}
public Iterable<Master> list() {
return masterRepository.findAll();
}
@Transactional // 确保批量保存操作在一个事务中
public Iterable<Master> save(List<Master> masters) {
// 实际应用中,您可能需要处理更新或删除旧数据,以避免重复或过时数据
// 例如:先清空表或根据业务ID进行更新
// masterRepository.deleteAll(); // 如果是全量更新
return masterRepository.saveAll(masters);
}
@Transactional
public Master save(Master master){
return masterRepository.save(master);
}
}注意: 在save(List<Master> masters)方法中,如果JSON文件是全量数据,您可能需要考虑在保存前清空现有数据或实现更复杂的更新策略,以避免数据重复。这里简单地使用了saveAll。
将文件读取和数据同步的逻辑封装到一个独立的Spring Service中,并使用@Scheduled注解实现周期性执行。
src/main/java/com/example/demo/Services/JsonFileProcessor.java
package com.example.demo.Services;
import com.example.demo.model.Master;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Service
public class JsonFileProcessor {
private final MasterService masterService;
private final ObjectMapper objectMapper;
private final ResourceLoader resourceLoader;
private final String jsonFilePath;
// 使用构造器注入所有依赖和配置值
public JsonFileProcessor(MasterService masterService,
ObjectMapper objectMapper,
ResourceLoader resourceLoader,
@Value("${app.data.json-file-path}") String jsonFilePath) {
this.masterService = masterService;
this.objectMapper = objectMapper;
this.resourceLoader = resourceLoader;
this.jsonFilePath = jsonFilePath;
}
@Scheduled(fixedRate = 90000) // 每90秒执行一次
public void processAndUpdateData() {
System.out.println("Scheduled task started: Reading JSON file from " + jsonFilePath);
try {
// 使用ResourceLoader获取资源
Resource resource = resourceLoader.getResource(jsonFilePath);
// 检查资源是否存在且可读
if (!resource.exists()) {
System.err.println("Error: JSON file not found at: " + jsonFilePath);
return;
}
if (!resource.isReadable()) {
System.err.println("Error: JSON file not readable at: " + jsonFilePath);
return;
}
// 使用try-with-resources确保InputStream被正确关闭
try (InputStream inputStream = resource.getInputStream()) {
TypeReference<List<Master>> typeReference = new TypeReference<List<Master>>() {};
List<Master> masters = objectMapper.readValue(inputStream, typeReference);
if (masters != null && !masters.isEmpty()) {
System.out.println("Read " + masters.size() + " records from JSON. First item: " + masters.get(0));
masterService.save(masters);
System.out.println("Saved " + masters.size() + " records to database.");
} else {
System.out.println("No records found in JSON file or file is empty.");
}
}
} catch (IOException e) {
System.err.println("Error processing JSON file '" + jsonFilePath +以上就是Spring Boot中动态读取并处理更新的JSON文件的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号