Get your Library API running and make your first API call in just a few minutes. This guide will walk you through setting up the database, starting the application, registering a user, and making authenticated requests.
Prerequisites
Before you begin, ensure you have the following installed:
Java 17 or higher
Maven 3.6+
PostgreSQL 12 or higher
Step 1: Set Up the Database
Create the PostgreSQL Database
First, create a PostgreSQL database named library: Or connect to PostgreSQL and run:
Configure Database Credentials
Update the database configuration in src/main/resources/application.properties: spring.datasource.url =jdbc:postgresql://localhost:5432/library
spring.datasource.username =postgres
spring.datasource.password =your_password
spring.datasource.driver-class-name =org.postgresql.Driver
spring.jpa.hibernate.ddl-auto =update
The application uses Hibernate’s ddl-auto=update to automatically create and update database tables on startup.
Step 2: Start the Application
Build the Project
Navigate to the project directory and build the application:
Run the Application
Start the Spring Boot application: The API will start on http://localhost:8080 by default. Look for the log message indicating the application has started successfully: Started LibraryapiApplication in X.XXX seconds
Step 3: Register Your First User
Create a user account to access protected endpoints:
curl -X POST http://localhost:8080/auth/register \
-H "Content-Type: application/json" \
-d '{
"firstname": "John",
"lastname": "Doe",
"email": "[email protected] ",
"password": "securePassword123"
}'
Expected Response (201 Created)
Error Response (400 Bad Request)
{
"id" : 1 ,
"firstname" : "John" ,
"lastname" : "Doe" ,
"email" : "[email protected] " ,
"borrowedBooks" : []
}
Passwords are hashed using BCrypt with a strength of 12 before being stored in the database.
Step 4: Authenticate Your Requests
The Library API uses HTTP Basic Authentication for protected endpoints. All requests to protected resources must include authentication credentials.
Login to Verify Credentials
Test your credentials with the login endpoint:
curl -X POST http://localhost:8080/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected] ",
"password": "securePassword123"
}'
Response:
Step 5: Make Your First Authenticated Request
Now that you’re registered, you can access protected endpoints using HTTP Basic Authentication.
Create a Book
curl -X PUT http://localhost:8080/books/978-0-123456-78-9 \
-u [email protected] :securePassword123 \
-H "Content-Type: application/json" \
-d '{
"isbn": "978-0-123456-78-9",
"title": "The Great Gatsby",
"author": {
"name": "F. Scott Fitzgerald",
"age": 44
},
"stock": 5
}'
Expected Response (201 Created)
{
"isbn" : "978-0-123456-78-9" ,
"title" : "The Great Gatsby" ,
"author" : {
"id" : 1 ,
"name" : "F. Scott Fitzgerald" ,
"age" : 44
},
"stock" : 5 ,
"rentals" : []
}
List All Books
Retrieve all books with pagination support:
curl -X GET "http://localhost:8080/books?page=0&size=10" \
-u [email protected] :securePassword123
Response:
{
"content" : [
{
"isbn" : "978-0-123456-78-9" ,
"title" : "The Great Gatsby" ,
"author" : {
"id" : 1 ,
"name" : "F. Scott Fitzgerald" ,
"age" : 44
},
"stock" : 5 ,
"rentals" : []
}
],
"pageable" : {
"pageNumber" : 0 ,
"pageSize" : 10
},
"totalElements" : 1 ,
"totalPages" : 1
}
Get a Specific Book
Retrieve details for a single book by ISBN:
curl -X GET http://localhost:8080/books/978-0-123456-78-9 \
-u [email protected] :securePassword123
Rent a Book
Create a rental for a user:
curl -X POST http://localhost:8080/books/978-0-123456-78-9/user/1 \
-u [email protected] :securePassword123
Expected Response (201 Created)
Error Response (400 Bad Request)
{
"id" : 1 ,
"user" : {
"id" : 1 ,
"firstname" : "John" ,
"lastname" : "Doe" ,
"email" : "[email protected] "
},
"book" : {
"isbn" : "978-0-123456-78-9" ,
"title" : "The Great Gatsby"
},
"loanDate" : "2026-03-04T10:30:00Z" ,
"returnDate" : "2026-03-18T10:30:00Z" ,
"returned" : false
}
When a book is rented, its stock is automatically decremented by 1. The default rental period is 2 weeks from the loan date.
What’s Next?
Now that you’ve successfully set up the Library API and made your first requests, explore more features:
API Reference Explore all available endpoints and their parameters
Authentication Learn about authentication and security configuration
Database Schema Understand the data models and relationships
Error Handling Handle errors and status codes properly
Common Issues
Connection refused to PostgreSQL
Ensure PostgreSQL is running and accessible on localhost:5432. Check your database credentials in application.properties. # Check if PostgreSQL is running
pg_isready
Make sure you’re including the correct email and password in your HTTP Basic Authentication headers. The credentials must match a registered user.
Another application is using port 8080. Either stop that application or change the port in application.properties:
For development, you can disable CSRF and configure security settings in SecurityConfig.java located at src/main/java/com/daudu/libraryapi/config/SecurityConfig.java:28