- Spring Initializr - Spring Initializr
- Gradle - Groovy: Package name =
peep.pea.blog
- Java: Version 21, Packaging = jar
- Spring Boot: Version 3.3.0
- Gradle - Groovy: Package name =
Ensure to include the following dependencies when setting up your project via Spring Initializr:
- Spring Web: For building web applications.
- Spring Data JPA: For database integration using Java Persistence API.
- Spring Security: For authentication and authorization.
- PostgreSQL Driver: To connect to PostgreSQL database.
- H2 Database: For in-memory database during development and testing.
- Lombok: To reduce boilerplate code like getters, setters.
- Entities: Define Java classes that map to database tables. Typical entities include
Post
,Comment
, andUser
. - Repositories: Utilize Spring Data JPA to automatically implement interfaces for database interactions.
- Services: Encapsulate business logic related to posts and comments within service classes. These handle transactions and ensure adherence to business rules.
- DTOs: Define Data Transfer Objects to safely transfer data between the API layer and the client, safeguarding sensitive information.
- REST Controllers: Manage HTTP requests and responses, interfacing with service classes to process business logic.
- Exception Handling: Employ
@ControllerAdvice
to globally manage exceptions and provide cleaner error responses.
- JWT for Authentication: Configure Spring Security to utilize JSON Web Tokens (JWTs) for stateless authentication.
- Authorization: Implement role-based access control to manage user access to resources based on their roles.
- JPA (Java Persistence API): Manages data interaction between Java objects and relational databases, with Spring Data JPA providing an abstraction layer that simplifies data manipulation.
- JDBC (Java Database Connectivity): The underlying technology for database interactions, managed by JPA implementations like Hibernate.
- H2 Database: Used for testing purposes due to its in-memory capabilities.
- PostgreSQL Driver: Connects your application to the PostgreSQL database.
- Lombok: Simplifies Java code by automatically generating getters, setters, and other common code.
The user
package in the application manages user information, including registration, profile updates, and authentication.
-
User.java: Represents the user entity with fields such as:
- username
- password
- role
- last online timestamp
- profile picture URL This record supports the straightforward transfer of data within the application.
-
UserController.java: The UserController handles CRUD operations on users. It includes methods for:
- Retrieving a single user by ID.
- Creating a new user.
- Listing all users with pagination.
The controller uses
Pageable
andUriComponentsBuilder
, which are best practices for RESTful APIs.
-
UserService.java: The UserService manages the business logic associated with user operations. It provides methods to:
- Create a user.
- Retrieve a user by ID.
- Update user details.
- Delete a user.
- Fetch all users with pagination. This service ensures that operations like updating user details respect the immutability of the user record by creating a new record.
-
UserRepository.java: Extends both
CrudRepository
andPagingAndSortingRepository
, providing comprehensive CRUD operations and pagination capabilities. It includes methods to:- Find users by ID and username.
- Save and delete users.
- Check for the existence of a username. This repository is integral to the data handling layer, directly interacting with the database.