This is a Spring Boot application that provides a REST API for managing products with pagination and sorting capabilities. The application uses a PostgreSQL database, which is configured and run using Docker Compose.
To run this application, ensure you have the following installed:
- Docker: Required to run the PostgreSQL database.
- Windows: Install Docker Desktop.
- macOS: Install Docker Desktop.
- Linux (e.g., Ubuntu): Install Docker and Docker Compose.
- Java 17 or later: Required to run the Spring Boot application.
- Maven: The project uses Maven for building and running. The Maven Wrapper (
./mvnw) is included, so you don't need to install Maven separately. - Git (optional): To clone the repository.
-
Clone the Repository:
git clone https://github.com/stuart7j/pagination-sorting.git cd pagination-sorting -
Verify Docker Installation: Ensure Docker and Docker Compose are installed and running:
docker --version docker-compose --version
For Docker Desktop on Windows or macOS, confirm the application is running.
The application uses a PostgreSQL database, configured in the docker-compose.yaml file.
-
Run Docker Compose: From the project root (where
docker-compose.yamlis located), start the PostgreSQL container in detached mode:docker-compose up -d
-
Verify the Container is Running: Check that the
pg_productscontainer is up:docker ps
You should see a container named
pg_productswith thepostgres:15image, listening on port5432. -
Database Configuration: The PostgreSQL database is configured with:
- Database Name:
productsdb - User:
postgres - Password:
postgres - Port:
5432(mapped tolocalhost:5432)
The application is preconfigured to connect to this database. No additional setup is needed unless you modify the
application.propertiesorapplication.ymlfile. - Database Name:
The project includes a Maven Wrapper (./mvnw), so you don't need to install Maven.
-
Run the Application: From the project root, execute:
./mvnw spring-boot:run
This command:
- Downloads dependencies (if not already cached).
- Compiles the code.
- Starts the Spring Boot application.
-
Verify the Application is Running: Once started, the application is available at
http://localhost:8059. Test the API using a tool like Postman or cURL:curl http://localhost:8059/api/products
This returns a paginated list of products sorted by
idin ascending order (default).
The application provides a REST API for managing products. Key endpoints include:
-
GET
/api/products: Retrieves a paginated and sorted list of products.-
Query Parameters:
page(default:0): Page number.size(default:10): Page size.sort(default:id,asc): Sort criteria (format:property,direction, e.g.,price,desc).category(optional): Filter by category.minPrice(optional): Filter by minimum price.maxPrice(optional): Filter by maximum price.
-
Example:
curl "http://localhost:8059/api/products?sort=price,desc&category=electronics"
-
-
Stop the Spring Boot Application: Press
Ctrl+Cin the terminal where./mvnw spring-boot:runis running. -
Stop the PostgreSQL Container: To stop and remove the Docker container:
docker-compose down
To also remove the database volume (deleting all data):
docker-compose down -v
-
PostgreSQL Container Not Starting:
-
Check if port
5432is in use:netstat -an | grep 5432If another service is using port
5432, stop it or change the port mapping indocker-compose.yaml(e.g.,5433:5432). -
Verify Docker is running: Ensure Docker Desktop (Windows/macOS) or the Docker daemon (Linux) is active.
-
-
Application Fails to Connect to Database:
-
Confirm the PostgreSQL container is running:
docker ps
-
Verify the database settings in
application.propertiesorapplication.yml:spring.datasource.url=jdbc:postgresql://localhost:5432/productsdb spring.datasource.username=postgres spring.datasource.password=postgres
-
Check application logs for errors:
tail -f target/spring-boot.log
-
-
API Returns Errors:
- For
Invalid sort parametererrors, ensure thesortparameter is in the formatproperty,direction(e.g.,id,asc). - Test with Postman or cURL to verify query parameters.
- For
-
Maven Issues:
-
If
./mvnw spring-boot:runfails, confirm Java 17+ is installed:java --version
-
Clear the Maven cache and retry:
./mvnw clean
-
src/main/java: Java source code, includingProductControllerandProductService.src/main/resources/application.properties: Configuration for the database and Spring Boot.docker-compose.yaml: Defines the PostgreSQL service.pom.xml: Maven configuration with dependencies.
- PostgreSQL data is persisted in a Docker volume (
pgdata). To reset the database, remove the volume withdocker-compose down -v. - The application runs on port
8059by default. To change this, updateserver.portinapplication.properties.