A Spring Boot REST API service for managing chat rooms and messages. This service handles all chat operations and validates JWT tokens from the auth service.
- Chat Room Management: Create, retrieve, and delete chat rooms
- Message Management: Send and retrieve messages within chat rooms
- JWT Authentication: Validates tokens from the Python auth service
- H2 In-Memory Database: No external database setup required
- REST API: Full REST endpoints for chat operations
- CORS Support: Configured for Angular frontend communication
- Java 17
- Spring Boot 3.1.5
- Spring Data JPA
- H2 Database
- JWT (JJWT)
- Maven
- Java 17 or higher
- Maven 3.6+
-
Navigate to the project directory:
cd chat-backend -
Build the project:
mvn clean install
-
Run the application:
mvn spring-boot:run
The service will start on http://localhost:8080/api
-
Create Room (requires JWT)
POST /api/rooms Authorization: Bearer <jwt_token> Content-Type: application/json { "name": "General", "description": "General discussion room" } -
Get All Rooms
GET /api/rooms -
Get Room by ID (requires JWT)
GET /api/rooms/{id} Authorization: Bearer <jwt_token> -
Delete Room (requires JWT)
DELETE /api/rooms/{id} Authorization: Bearer <jwt_token>
-
Send Message (requires JWT)
POST /api/messages Authorization: Bearer <jwt_token> Content-Type: application/json { "roomId": 1, "content": "Hello everyone!" } -
Get Messages by Room
GET /api/messages?roomId=1 -
Get Message by ID (requires JWT)
GET /api/messages/{id} Authorization: Bearer <jwt_token> -
Delete Message (requires JWT)
DELETE /api/messages/{id} Authorization: Bearer <jwt_token>
- Verify Token (called by Auth Service)
GET /api/auth/verify Authorization: Bearer <jwt_token>
Edit src/main/resources/application.yml to customize:
server:
port: 8080 # Change port if needed
servlet:
context-path: /api # API base path
jwt:
secret: your-secret-key # Change this in production!
expiration: 86400000 # 24 hours in milliseconds
auth-service:
url: http://localhost:8000 # Auth service URLThe application uses H2 in-memory database. Access the H2 console at:
http://localhost:8080/api/h2-console
Default credentials:
- JDBC URL:
jdbc:h2:mem:chatdb - Username:
sa - Password: (leave blank)
This service validates JWT tokens and can verify users through the auth service:
- Frontend → Sends JWT in
Authorization: Bearer <token>header - Backend → Validates JWT signature using configured secret
- Auth Service → Can be called by backend for additional verification
chat-backend/
├── pom.xml
├── src/main/
│ ├── java/com/chatapp/
│ │ ├── ChatBackendApplication.java
│ │ ├── config/
│ │ │ ├── JwtConfig.java
│ │ │ ├── JwtUtil.java
│ │ │ └── CorsConfig.java
│ │ ├── controllers/
│ │ │ ├── ChatRoomController.java
│ │ │ ├── MessageController.java
│ │ │ └── AuthController.java
│ │ ├── services/
│ │ │ ├── ChatRoomService.java
│ │ │ ├── MessageService.java
│ │ │ └── AuthVerificationService.java
│ │ ├── repositories/
│ │ │ ├── ChatRoomRepository.java
│ │ │ └── MessageRepository.java
│ │ └── models/
│ │ ├── ChatRoom.java
│ │ └── Message.java
│ └── resources/
│ └── application.yml
└── README.md
Port 8080 already in use?
# Change port in application.yml or run with:
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081"JWT validation failing?
- Ensure the JWT secret matches the auth service
- Check token hasn't expired
- Verify token format:
Bearer <token>
CORS errors?
- Frontend must be running on
http://localhost:4200 - Adjust CORS settings in
CorsConfig.javaif needed
- All timestamps are stored in UTC
- Messages are ordered by creation time
- Room names must be unique
- User information is extracted from JWT claims
MIT