https://github.com/capella-marcosfilipe/marcoscapella-blog-api-java
REST API for marcoscapella blog made in Java Spring
https://github.com/capella-marcosfilipe/marcoscapella-blog-api-java
java postgresql rest-api spring-boot
Last synced: 3 months ago
JSON representation
REST API for marcoscapella blog made in Java Spring
- Host: GitHub
- URL: https://github.com/capella-marcosfilipe/marcoscapella-blog-api-java
- Owner: capella-marcosfilipe
- License: mit
- Created: 2025-04-07T22:48:05.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-11T14:32:02.000Z (about 1 year ago)
- Last Synced: 2025-04-15T16:51:13.968Z (about 1 year ago)
- Topics: java, postgresql, rest-api, spring-boot
- Language: Java
- Homepage:
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐ A Java REST API for a Simple Personal Blog
This is the backend for a simple blog application, built with **Spring Boot**. It exposes a RESTful API that handles blog posts, tags, and authors. The frontend is built using **Angular** and communicates with this API.
---
## ๐ Features
- Create, read, update, and delete blog posts
- Associate posts with multiple tags (many-to-many relationship)
- Link posts to authors (many-to-one relationship)
- Generate slugs automatically from post titles
- Automatically create and associate tags on post creation
- Securely expose selected user information in responses
- Ready for deployment on [Render](https://render.com)
---
## ๐ ๏ธ Tech Stack
- Java 17+
- Spring Boot 3+
- Spring Data JPA
- PostgreSQL
- Angular (Frontend)
- Maven
---
## ๐ฆ Endpoints Overview
### ๐ Posts
| Method | Endpoint | Description |
|--------|----------------------|--------------------------------------|
| GET | `/posts` | Get all posts |
| GET | `/posts/{id}` | Get a post by ID |
| GET | `/posts/slug/{slug}` | Get a post by slug |
| POST | `/posts` | Create a new post |
| PUT | `/posts` | Update a post |
| PATCH | `/posts/{id}` | Update a post partially
| DELETE | `/posts/{id}` | Delete a post by ID |
---
### ๐ท๏ธ Tags
| Method | Endpoint | Description |
|--------|--------------------|--------------------------------------|
| GET | `/tags` | Get all tags |
| GET | `/tags/{name}` | Get a tag by name |
| POST | `/tags` | Create a new tag |
| DELETE | `/tags/{id}` | Delete a tag by ID |
---
### ๐ค Users
| Method | Endpoint | Description |
|--------|------------------------------|--------------------------------------|
| GET | `/users` | Get all users |
| GET | `/users/{id}` | Get user by ID |
| GET | `/users/email/{email}` | Get user by email |
| GET | `/users/username/{username}` | Get user by username |
| POST | `/users` | Create a new user |
| PUT | `/users/{id}` | Update a user |
| DELETE | `/users/{id}` | Delete a user |
---
### โค๏ธ Health Check
| Method | Endpoint | Description |
|--------|--------------|------------------------|
| GET | `/health` | Returns `OK` if running|
---
## ๐ค Request/Response Format
Request/Response format uses DTOs for better control and separation of concerns.
### Sample JSON (POST `/api/posts`)
```json
{
"title": "Building a Career You Love",
"content": "It takes time, effort, and alignment...",
"published": true,
"author": { "id": 1 },
"tags": [
{ "id": 1 },
{ "id": 2 }
]
}
```
---
## ๐งช Running Locally
### Prerequisites
- Java 17+
- Maven
- PostgreSQL running locally or via Docker
### 1. Clone the project
### 2. Configure environment variables
In `src/main/resources/application.properties`, reference your hidden environment variables like this:
```properties
spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASS}
```
On **Render**, you can set these variables securely in the dashboard.
For local development, you can use a `.env` file with tools like [dotenv-spring](https://github.com/cdimascio/dotenv-java) or export them manually:
```bash
export DB_URL=jdbc:postgresql://localhost:5432/blogdb
export DB_USER=your_user
export DB_PASS=your_password
```
### 3. Run the app
```bash
./mvnw spring-boot:run
```
Server will start at `http://localhost:8080`.
---
## ๐ CORS Configuration
CORS is enabled in `WebConfig.java` to allow requests from your Angular frontend:
```java
.allowedOrigins("http://localhost:4200")
```
Make sure to update it for production.
---
## โ๏ธ JPA Notes
- Some endpoints use `JOIN FETCH` queries to avoid `LazyInitializationException`.
- Relationships like Post -> Tags are lazily loaded by default for performance.
- `@Transactional` annotations may be used in service methods to control session scope.
---
## ๐ฆ Deployment on Render
- Create a PostgreSQL instance
- Add environment variables in the Render dashboard:
- `DB_URL`
- `DB_USER`
- `DB_PASS`
- Deploy as a Java service using:
- Build Command: `./mvnw clean package`
- Start Command: `java -jar target/*.jar`
---
## โ๏ธ Author
Developed by [**Marcos Capella**](https://marcoscapella.com.br)
Feel free to connect: [LinkedIn](https://linkedin.com/in/marcoscapella)