A comprehensive Spring Boot REST API application for a social media platform with full CRUD operations for posts, comments, and likes.
This is a production-ready Spring Boot application built with the following specifications:
- Package Name:
com.contoso.socialapp - Artifact ID:
socialapp - Group ID:
com.contoso - Package Type:
jar - Java Version: OpenJDK 21
- Build Tool: Gradle
- Database: SQLite (embedded)
- Port: 8080
- Spring Boot 3.2.5: Core framework
- Spring Web: RESTful API endpoints
- Spring Data JPA: Database operations
- Spring Boot Actuator: Application monitoring
- Spring Boot Validation: Input validation
- SQLite: Embedded database
- Hibernate Community Dialects: SQLite support
- Springdoc OpenAPI: API documentation (Swagger UI)
- Lombok: Boilerplate code reduction
src/
├── main/
│ ├── java/
│ │ └── com/
│ │ └── contoso/
│ │ └── socialapp/
│ │ ├── SocialAppApplication.java # Main application class
│ │ ├── config/
│ │ │ ├── WebConfig.java # CORS configuration
│ │ │ └── OpenApiConfig.java # Swagger/OpenAPI config
│ │ ├── controller/
│ │ │ ├── HealthController.java # Health endpoints
│ │ │ ├── PostController.java # Post management
│ │ │ └── CommentController.java # Comment & like management
│ │ ├── model/
│ │ │ ├── Post.java # Post entity
│ │ │ ├── Comment.java # Comment entity
│ │ │ ├── Like.java # Like entity
│ │ │ └── dto/ # Data Transfer Objects
│ │ ├── repository/
│ │ │ ├── PostRepository.java # Post data access
│ │ │ ├── CommentRepository.java # Comment data access
│ │ │ └── LikeRepository.java # Like data access
│ │ └── service/
│ │ ├── PostService.java # Post business logic
│ │ └── CommentService.java # Comment business logic
│ └── resources/
│ ├── application.properties # Application configuration
│ └── data.sql # Sample data (optional)
└── test/
└── java/
└── com/
└── contoso/
└── socialapp/
└── SocialAppApplicationTests.java # Integration tests
- ✅ Complete RESTful API for social media operations
- ✅ Post management (Create, Read, Update, Delete)
- ✅ Comment system with full CRUD operations
- ✅ Like/Unlike functionality
- ✅ SQLite database with JPA/Hibernate
- ✅ OpenAPI/Swagger documentation
- ✅ CORS enabled for localhost and GitHub Codespaces
- ✅ Dynamic server URL configuration
- ✅ Health check endpoints
- ✅ Spring Boot Actuator integration
- ✅ Comprehensive error handling
- ✅ Input validation with Bean Validation
Refer to the README doc for preparation.
First, set the environment variable of $REPOSITORY_ROOT.
# bash/zsh
REPOSITORY_ROOT=$(git rev-parse --show-toplevel)# PowerShell
$REPOSITORY_ROOT = git rev-parse --show-toplevelThen, navigate to the java directory.
cd $REPOSITORY_ROOT/complete/java# Make gradlew executable (if needed)
chmod +x ./gradlew
# Build the project
./gradlew build# Start the application using Gradle
./gradlew bootRun
# Alternative: Run the JAR file directly
# java -jar build/libs/socialapp-0.0.1-SNAPSHOT.jar# Check health endpoint
curl http://localhost:8080/api/health
# Expected response: {"status":"healthy"}Open your browser and navigate to:
- Swagger UI: http://localhost:8080/swagger-ui.html
- OpenAPI JSON: http://localhost:8080/v3/api-docs
GET /api/health- Custom health check endpointGET /api/welcome- Welcome message endpoint
GET /api/posts- Get all postsGET /api/posts/{id}- Get specific post by IDPOST /api/posts- Create a new postPATCH /api/posts/{id}- Update an existing postDELETE /api/posts/{id}- Delete a post
GET /api/posts/{postId}/comments- Get all comments for a postGET /api/posts/{postId}/comments/{commentId}- Get specific commentPOST /api/posts/{postId}/comments- Add a comment to a postPATCH /api/posts/{postId}/comments/{commentId}- Update a commentDELETE /api/posts/{postId}/comments/{commentId}- Delete a comment
POST /api/posts/{postId}/like- Like a postDELETE /api/posts/{postId}/like- Unlike a post
GET /actuator/health- Spring Boot health indicatorGET /actuator/info- Application information
curl -X POST http://localhost:8080/api/posts \
-H "Content-Type: application/json" \
-d '{
"title": "My First Post",
"content": "This is the content of my first post!",
"authorName": "John Doe"
}'curl http://localhost:8080/api/postscurl -X POST http://localhost:8080/api/posts/1/comments \
-H "Content-Type: application/json" \
-d '{
"content": "Great post!",
"authorName": "Jane Smith"
}'curl -X POST http://localhost:8080/api/posts/1/like \
-H "Content-Type: application/json" \
-d '{
"userName": "john_doe"
}'- Open http://localhost:8080/swagger-ui.html
- Explore available endpoints
- Click "Try it out" on any endpoint
- Fill in parameters and click "Execute"
# Run all tests
./gradlew test
# Run with coverage report
./gradlew test jacocoTestReport
# Run specific test class
./gradlew test --tests "SocialAppApplicationTests"The application uses SQLite as an embedded database:
- Database file:
sns_api.db(created automatically) - Location: Project root directory
- Schema: Auto-generated by Hibernate
- Sample data: Loaded from
data.sql(if present)
To reset the database, simply delete the sns_api.db file and restart the application.
Key configuration settings in application.properties:
# Application Settings
spring.application.name=socialapp
server.port=8080
# Database Configuration
spring.datasource.url=jdbc:sqlite:sns_api.db
spring.jpa.hibernate.ddl-auto=update
# OpenAPI/Swagger Configuration
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.swagger-ui.operationsSorter=methodThe application supports both localhost and GitHub Codespaces:
- Localhost:
http://localhost:8080 - GitHub Codespaces: Auto-detected and configured dynamically
The application automatically detects the runtime environment:
- Local Development: Uses
http://localhost:8080 - GitHub Codespaces: Uses
https://{codespace-name}-8080.{domain}
# Create production JAR
./gradlew clean build
# JAR location
ls -la build/libs/socialapp-0.0.1-SNAPSHOT.jar# Run with production profile
java -jar build/libs/socialapp-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
# Or with custom port
java -jar build/libs/socialapp-0.0.1-SNAPSHOT.jar --server.port=8081# Find process using port 8080
lsof -i :8080
# Kill the process (replace PID)
kill -9 <PID>
# Or use a different port
./gradlew bootRun --args='--server.port=8081'# Clean and rebuild
./gradlew clean build
# Update Gradle wrapper
./gradlew wrapper --gradle-version=8.5# Reset database
rm sns_api.db
./gradlew bootRun- Application logs: Console output when running
./gradlew bootRun - Health check:
GET /actuator/health - Application info:
GET /actuator/info
- CORS enabled for all origins
- SQLite database (not suitable for production scale)
- No authentication/authorization
For production deployment, consider:
- Restricting CORS to specific domains
- Using PostgreSQL/MySQL instead of SQLite
- Implementing Spring Security for authentication
- Adding rate limiting and input sanitization
- Using HTTPS/TLS encryption