Anıl Şenocak

This project demonstrates how to implement a lightweight, thread-safe, and local JSON file-based data store directly inside a Spring Boot application, maintaining the standard repository pattern design.
When developing standalone microservices, tools, or fast prototypes, spinning up a heavy relational database or managing external Docker containers just to persist a few records introduces unwanted friction.
We need a persistent data store that is:
The Spring Data File Repository abstracts file persistence in a way similar to Spring Data JPA, but instead of database tables, it works with file storage backends (local disk, S3-compatible storage, or custom implementations).
You define repositories like:
@Repository
public interface ParkRepository extends JsonRepository<Park, Long> {
List<Park> findAll();
Page<Park> findAllByOrderByParkIDAsc(Pageable pageable);
Page<Park> findAllByParkNameContaining(String parkName, Pageable pageable);
}
Core operations:
save(file)saveAll(files)findById(id)existsById(id)findAll()findAllById(id)count()deleteById(id)delete(file)deleteAllById(id)deleteAll(files)Also the great feature that comes from Spring Data which is derived methods that examples shown above. Internally, these behave like Spring Data repositories but delegate to storage adapters.
This library provides a fully functional, generic repository pattern backed by local JSON file storage. It leverages Jackson for high-performance serialization, thread-safe memory caches for lookups, and explicit file-level locking mechanisms to prevent corruption.
[ Client / Controller ]
│
▼
[ Service Layer ] (Validation / Business Rules)
│
▼
[ Repository Layer] ──▶ [ In-Memory Cache (CopyOnWriteArrayList)]
│
▼
[ JsonFileStore] ──▶ [ ReadWriteLock Protection ] ──▶ [ Local .json File ]
ReadWriteLock to ensure multiple threads can read simultaneously, while exclusive access is guaranteed during a write phase.ObjectMapper for robust type-safe serialization/deserialization.CopyOnWriteArrayList acting as a lightning-fast in-memory cache layer.@PostConstruct.Externalizing parameters is essential. Define your application storage path easily inside your src/main/resources/application.yml:
spring:
data:
json:
file-path: ${user.dir}/example/data
The system natively supports multiple entity structures. The repository directory contains pre-configured implementations for both User and Product models. To add your own custom entity; Define your model class in the model/package. Build a repository extending the generic file store pattern. Expose your business requirements through your service and controller layer.
JSON files are stored in the data/ directory:
data/users.json - User datadata/products.json - Product data (if used)To add a new entity (e.g., Note, Product):
model/ packagerepository/ package (similar to UserRepository)service/ package (optional)controller/ package (optional)Example: The Product entity is already included as a reference implementation.
GitHub - senocak/Spring-Data-File-Repository
Happy coding!