A microservice for storing and serving code snippets. Built with Spring Boot, PostgreSQL, and Java 17.
Snippet Service is a RESTful API that allows you to create, read, update, and delete code snippets. Each snippet contains a title, code content, and programming language information. The service provides comprehensive API documentation through integrated Swagger/OpenAPI support.
- Create Snippets: Store code snippets with title, code, and language information
- Retrieve Snippets: Fetch all snippets or get a specific snippet by ID
- Update Snippets: Modify existing snippet content and metadata
- Delete Snippets: Remove snippets from the database
- API Documentation: Interactive Swagger/OpenAPI UI for easy API exploration
- Exception Handling: Global exception handling for consistent error responses
- Security-Ready: Configuration structure in place for Spring Security integration
- Java: Version 17
- Spring Boot: Version 4.1.0-SNAPSHOT
- Database: PostgreSQL
- ORM: Spring Data JPA / Hibernate
- API Documentation: SpringDoc OpenAPI 3.0.2
- Build Tool: Apache Maven
- Additional Tools: Lombok, Spring DevTools
- Java 17 or higher
- PostgreSQL 12 or higher
- Maven 3.6 or higher
git clone <repository-url>
cd snippetserviceCreate a PostgreSQL database for the application:
CREATE DATABASE snippets;Update src/main/resources/application.properties with your PostgreSQL credentials:
spring.datasource.url=jdbc:postgresql://localhost:5432/snippets
spring.datasource.username=your_username
spring.datasource.password=your_passwordmvn clean installmvn spring-boot:runThe application will start on http://localhost:8080
All endpoints are prefixed with /api/snippets
POST /api/snippets
Request body:
{
"title": "Quick Sort Algorithm",
"code": "public void quickSort(int[] arr) { ... }",
"language": "java"
}Response:
{
"id": 1,
"title": "Quick Sort Algorithm",
"code": "public void quickSort(int[] arr) { ... }",
"language": "java"
}GET /api/snippets
Returns a list of all stored snippets.
GET /api/snippets/{id}
Returns the snippet with the specified ID.
PUT /api/snippets/{id}
Request body:
{
"title": "Updated Title",
"code": "Updated code content",
"language": "python"
}DELETE /api/snippets/{id}
Deletes the snippet with the specified ID.
Once the application is running, you can access the interactive API documentation at:
http://localhost:8080/swagger-ui.html
This provides a user-friendly interface to explore and test all available endpoints.
snippetservice/
├── src/
│ ├── main/
│ │ ├── java/com/snippet/snippetservice/
│ │ │ ├── SnippetserviceApplication.java # Main Spring Boot application
│ │ │ ├── SecurityConfig.java # Security configuration
│ │ │ ├── controller/
│ │ │ │ ├── HealthController.java # Health check endpoint
│ │ │ │ └── SnippetController.java # Snippet API endpoints
│ │ │ ├── service/
│ │ │ │ └── SnippetService.java # Business logic
│ │ │ ├── repository/
│ │ │ │ └── SnippetRepository.java # Database access
│ │ │ ├── model/
│ │ │ │ └── Snippet.java # Data model
│ │ │ └── exception/
│ │ │ ├── GlobalExceptionHandler.java # Exception handling
│ │ │ └── SnippetNotFoundException.java # Custom exception
│ │ └── resources/
│ │ └── application.properties # Application configuration
│ └── test/
│ └── java/ # Test files
├── pom.xml # Maven configuration
└── README.md # This file
Key configuration properties in application.properties:
| Property | Default | Description |
|---|---|---|
spring.application.name |
snippetservice | Application name |
server.port |
8080 | Server port |
spring.datasource.url |
jdbc:postgresql://localhost:5432/snippets | Database URL |
spring.datasource.username |
dev | Database username |
spring.datasource.password |
dev | Database password |
spring.jpa.hibernate.ddl-auto |
update | Hibernate DDL auto strategy |
spring.jpa.show-sql |
true | Show SQL in logs |
Create an executable JAR file:
mvn clean packageRun the packaged application:
java -jar target/snippetservice-0.0.1-SNAPSHOT.jarThe project includes Spring DevTools for automatic application restart during development. Simply rebuild your project in your IDE.
- Spring Security integration (commented in
pom.xml) - Authentication and authorization
- User management
- Snippet tagging and categorization
- Full-text search capabilities
- Snippet version history
The application includes a global exception handler that provides consistent error responses. When a requested snippet is not found, the API returns a 404 status with an appropriate error message.
A health check endpoint is available for monitoring:
GET /api/health
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
This project is provided as-is for educational and development purposes.
For issues or questions, please create an issue in the repository or contact the development team.
Last Updated: March 2026
Java Version: 17
Spring Boot Version: 4.1.0-SNAPSHOT