This is a Spring Boot application demonstrating comprehensive entity auditing using both JaVers and Spring Data JPA Auditing. It provides a simple REST API for managing Post
entities and allows retrieving their detailed change history.
- CRUD Operations: Standard RESTful endpoints for Creating, Reading, Updating, and Deleting
Post
entities. - JaVers Auditing: Automatic, fine-grained auditing of changes to
Post
entities (creations, updates, deletions) managed by JaVers.- Captures snapshots of entity state at each change.
- Tracks changes to specific fields (
title
,content
).
- Spring Data JPA Auditing: Standard auditing for creation/modification metadata (
createdBy
,creationDate
,lastModifiedBy
,lastModifiedDate
) using Spring Data JPA's built-in features. - Audit History API: An endpoint (
GET /posts/{id}/histories
) to retrieve the complete version history of a specificPost
, showing changes, responsible user, and state at each version. - User Tracking: Integrates with Spring Security to automatically capture the username of the authenticated user performing the changes for both JaVers commits and JPA audit fields.
- Custom Commit Metadata: Demonstrates adding custom metadata (e.g.,
tenant
) to JaVers commits usingCommitPropertiesProvider
. - DTO Pattern: Uses Data Transfer Objects (
PostDto
,HistoryDto
) for clean API contracts. - MapStruct Mapping: Leverages MapStruct for efficient and type-safe mapping between Entities and DTOs.
- Spring Security Integration: Basic security setup with in-memory users for authentication.
- Pagination: Supports paginated retrieval of
Post
entities (GET /posts/
). - Clear Separation: Demonstrates how JaVers and Spring Data JPA Auditing can coexist, with JPA auditing fields ignored by JaVers (
@DiffIgnore
).
- Java (JDK 17+)
- Spring Boot 3.x
- Spring Data JPA
- Spring Security
- JaVers (Core, Spring Boot Starter Data JPA)
- PostgreSQL (or any compatible SQL database)
- MapStruct
- Lombok
- Maven / Gradle
-
Clone the Repository:
git clone <your-repository-url> cd <repository-directory>
-
Database Setup:
- Ensure you have a PostgreSQL database running.
- Create a database named
mydatabase
. - Create a user
myuser
with passwordsecret
and grant necessary permissions on themydatabase
. - Alternatively, update the database connection details in
src/main/resources/application.properties
:spring.datasource.url=jdbc:postgresql://<your_db_host>:<your_db_port>/<your_db_name> spring.datasource.username=<your_db_user> spring.datasource.password=<your_db_password>
- JaVers requires its own tables to store audit data. These tables (
jv_global_id
,jv_commit
,jv_commit_property
,jv_snapshot
) will be created or updated automatically on application startup ifspring.jpa.hibernate.ddl-auto
is set toupdate
orcreate
.
-
Build the Application:
- Using Maven:
mvn clean install
- Using Gradle:
./gradlew clean build
- Using Maven:
-
Run the Application:
- Using Maven:
mvn spring-boot:run
- Using Gradle:
./gradlew bootRun
- Or run the JAR file:
java -jar target/javers-auditing-*.jar
- Using Maven:
The application will start on http://localhost:8080
(or the configured port).
The primary endpoints are under /posts
:
POST /posts
: Create a new post. Requires authentication.- Body:
PostDto
({"title": "...", "content": "..."}
)
- Body:
GET /posts/{id}
: Get a specific post by ID. Requires authentication.PUT /posts/{id}
: Update an existing post. Requires authentication.- Body:
PostDto
({"title": "...", "content": "..."}
)
- Body:
DELETE /posts/{id}
: Delete a post by ID. Requires authentication.GET /posts/
: Get a paginated list of posts. Requires authentication.- Supports standard Spring Data pagination parameters (e.g.,
?page=0&size=10&sort=title,asc
).
- Supports standard Spring Data pagination parameters (e.g.,
GET /posts/{id}/histories
: Get the audit history for a specific post. Requires authentication.- Returns a list of
HistoryDto
.
- Returns a list of
Authentication:
The application uses basic authentication or form login configured via Spring Security. Default users (defined in SecurityConfig
):
- Username:
Youssef
, Password:1234
- Username:
Admin
, Password:Admin
Use these credentials when prompted by your HTTP client (like curl
, Postman, or your browser).
- JaVers: The
PostRepo
is annotated with@JaversSpringDataAuditable
, enabling JaVers to automatically interceptsave()
anddeleteById()
calls to create audit snapshots. TheJaversAuthorProvider
bean fetches the current username from Spring Security for theauthor
field in JaVers commits. TheCommitPropertiesProvider
adds extra context (like"tenant":"tenant-1"
) to each commit. - Spring Data JPA: The
Post
entity uses@EntityListeners(AuditingEntityListener.class)
and fields annotated with@CreatedBy
,@CreatedDate
,@LastModifiedBy
,@LastModifiedDate
. TheSecurityAuditorAware
bean provides the username for the@CreatedBy
and@LastModifiedBy
fields. These fields are marked with@DiffIgnore
so JaVers doesn't track their changes directly, avoiding redundancy. - History Retrieval: The
HistoryController
uses theJaversHistoryServiceImpl
which queries the JaVers repository (javers.findSnapshots
) to get the historicalCdoSnapshot
objects for a given entity instance and maps them toHistoryDto
.
List of Key Features:
- CRUD Operations: Full Create, Read, Update, Delete for
Post
entities via REST API. - JaVers Auditing: Automatic snapshotting of
Post
entity changes (Create, Update, Delete). - Spring Data JPA Auditing: Tracks creation/modification user and timestamp (
createdBy
,creationDate
, etc.). - Combined Auditing Strategy: Demonstrates using both JaVers (for detailed field changes) and JPA Auditing (for metadata) effectively.
- Audit History API: Dedicated endpoint to retrieve the full version history of any
Post
. - User Attribution: Automatically records the authenticated user performing changes in both audit logs (JaVers author, JPA createdBy/lastModifiedBy).
- Custom JaVers Metadata: Ability to add custom key-value pairs (like
tenant
) to JaVers audit commits. - DTO Pattern & MapStruct: Clean API contracts and efficient entity-DTO mapping.
- Spring Security Integration: Basic authentication protecting API endpoints.
- Pagination Support: Allows fetching lists of posts in pages.
- Configuration Driven: Database and basic settings managed via
application.properties
. - Clear Code Structure: Well-organized packages for controllers, services, entities, repositories, mappers, and configuration.