|
1 | | -## Plain REST API CRUD with Spring Boot 3 and PostgreSQL. |
| 1 | +# Spring Boot REST API with PostgreSQL |
2 | 2 |
|
3 | | -[](https://travis-ci.org/OKaluzny/spring-boot-rest-api-postgresql) |
| 3 | + |
4 | 4 |
|
5 | | -### Technology stack: |
| 5 | +REST API CRUD application built with Spring Boot 4 and PostgreSQL. |
6 | 6 |
|
7 | | -* Spring Boot 3; |
8 | | -* Spring Web; |
9 | | -* Spring Data; |
10 | | -* PostgreSQL database; |
11 | | -* Hibernate; |
12 | | -* Lombok; |
13 | | -* Spring Security (as basic authentication). |
| 7 | +## Tech Stack |
14 | 8 |
|
15 | | -#### To run this application use: |
| 9 | +- Java 17 |
| 10 | +- Spring Boot 4.0.2 |
| 11 | +- Spring Data JPA |
| 12 | +- Spring Security (Basic Auth) |
| 13 | +- PostgreSQL |
| 14 | +- Lombok |
| 15 | +- Docker |
| 16 | + |
| 17 | +## Requirements |
| 18 | + |
| 19 | +- JDK 17+ |
| 20 | +- Maven 3.8+ |
| 21 | +- PostgreSQL 14+ (or Docker) |
| 22 | + |
| 23 | +## Quick Start |
| 24 | + |
| 25 | +### 1. Start PostgreSQL |
| 26 | + |
| 27 | +```bash |
| 28 | +docker-compose up -d postgres |
| 29 | +``` |
| 30 | + |
| 31 | +### 2. Run Application |
16 | 32 |
|
17 | 33 | ```bash |
18 | 34 | mvn spring-boot:run |
19 | 35 | ``` |
20 | 36 |
|
21 | | -### The view in the Postman: |
22 | | -[http://localhost:8080/api/books](http://localhost:8080/api/books) |
| 37 | +Application starts at `http://localhost:8080` |
| 38 | + |
| 39 | +### 3. Default Credentials |
| 40 | + |
| 41 | +- **API Auth:** `user:user` |
| 42 | +- **Database:** `postgres:postgres` |
| 43 | + |
| 44 | +## API Endpoints |
| 45 | + |
| 46 | +| Method | Endpoint | Description | |
| 47 | +|--------|----------|-------------| |
| 48 | +| POST | `/api/books` | Create book | |
| 49 | +| GET | `/api/books` | Get all books | |
| 50 | +| GET | `/api/books/{id}` | Get book by ID | |
| 51 | +| GET | `/api/books?name={name}` | Search by name | |
| 52 | +| PUT | `/api/books/{id}` | Update book | |
| 53 | +| DELETE | `/api/books/{id}` | Delete book | |
| 54 | +| DELETE | `/api/books` | Delete all books | |
| 55 | + |
| 56 | +### Example Request |
| 57 | + |
| 58 | +```bash |
| 59 | +curl -X POST http://localhost:8080/api/books \ |
| 60 | + -H "Content-Type: application/json" \ |
| 61 | + -H "Authorization: Basic dXNlcjp1c2Vy" \ |
| 62 | + -d '{ |
| 63 | + "name": "Java Programming", |
| 64 | + "description": "Learn Java", |
| 65 | + "tags": ["java", "programming"] |
| 66 | + }' |
| 67 | +``` |
| 68 | + |
| 69 | +### Example Response |
| 70 | + |
| 71 | +```json |
| 72 | +{ |
| 73 | + "id": 1, |
| 74 | + "name": "Java Programming", |
| 75 | + "description": "Learn Java", |
| 76 | + "tags": ["java", "programming"] |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## Configuration |
| 81 | + |
| 82 | +Environment variables: |
| 83 | + |
| 84 | +| Variable | Default | Description | |
| 85 | +|----------|---------|-------------| |
| 86 | +| `DB_HOST` | localhost | Database host | |
| 87 | +| `DB_PORT` | 5432 | Database port | |
| 88 | +| `DB_NAME` | book_db | Database name | |
| 89 | +| `DB_USERNAME` | postgres | Database user | |
| 90 | +| `DB_PASSWORD` | postgres | Database password | |
| 91 | +| `SECURITY_USER` | user | API username | |
| 92 | +| `SECURITY_PASSWORD` | user | API password | |
| 93 | +| `SERVER_PORT` | 8080 | Application port | |
| 94 | + |
| 95 | +## Docker |
| 96 | + |
| 97 | +### Run full stack |
| 98 | + |
| 99 | +```bash |
| 100 | +docker-compose --profile full up -d |
| 101 | +``` |
| 102 | + |
| 103 | +### Run only PostgreSQL |
| 104 | + |
| 105 | +```bash |
| 106 | +docker-compose up -d postgres |
| 107 | +``` |
| 108 | + |
| 109 | +### Build image only |
| 110 | + |
| 111 | +```bash |
| 112 | +docker build -t book-api . |
| 113 | +``` |
| 114 | + |
| 115 | +## Actuator Endpoints |
| 116 | + |
| 117 | +- Health: `GET /actuator/health` |
| 118 | +- Info: `GET /actuator/info` |
| 119 | +- Metrics: `GET /actuator/metrics` |
| 120 | + |
| 121 | +## Project Structure |
| 122 | + |
| 123 | +``` |
| 124 | +src/main/java/com/kaluzny/ |
| 125 | +├── Application.java # Entry point |
| 126 | +├── domain/ |
| 127 | +│ ├── Book.java # JPA Entity |
| 128 | +│ └── BookRepository.java # Spring Data Repository |
| 129 | +├── exception/ |
| 130 | +│ ├── BookNotFoundException.java |
| 131 | +│ └── GlobalExceptionHandler.java |
| 132 | +└── web/ |
| 133 | + └── BookRestController.java # REST Controller |
| 134 | +``` |
| 135 | + |
| 136 | +## Testing |
| 137 | + |
| 138 | +```bash |
| 139 | +# Run tests (requires PostgreSQL) |
| 140 | +mvn test |
| 141 | + |
| 142 | +# Skip tests |
| 143 | +mvn package -DskipTests |
| 144 | +``` |
| 145 | + |
| 146 | +## License |
| 147 | + |
| 148 | +MIT |
0 commit comments