Skip to content

juniorbeshu/snppetservice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snippet Service

A microservice for storing and serving code snippets. Built with Spring Boot, PostgreSQL, and Java 17.

Overview

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.

Features

  • 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

Tech Stack

  • 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

Prerequisites

  • Java 17 or higher
  • PostgreSQL 12 or higher
  • Maven 3.6 or higher

Installation & Setup

1. Clone the Repository

git clone <repository-url>
cd snippetservice

2. Database Setup

Create a PostgreSQL database for the application:

CREATE DATABASE snippets;

3. Configure Application Properties

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_password

4. Build the Project

mvn clean install

5. Run the Application

mvn spring-boot:run

The application will start on http://localhost:8080

API Endpoints

All endpoints are prefixed with /api/snippets

Create a Snippet

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 All Snippets

GET /api/snippets

Returns a list of all stored snippets.

Get Snippet by ID

GET /api/snippets/{id}

Returns the snippet with the specified ID.

Update a Snippet

PUT /api/snippets/{id}

Request body:

{
  "title": "Updated Title",
  "code": "Updated code content",
  "language": "python"
}

Delete a Snippet

DELETE /api/snippets/{id}

Deletes the snippet with the specified ID.

Swagger/OpenAPI Documentation

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.

Project Structure

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

Configuration

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

Building for Production

Create an executable JAR file:

mvn clean package

Run the packaged application:

java -jar target/snippetservice-0.0.1-SNAPSHOT.jar

Development

Enable Hot Reload

The project includes Spring DevTools for automatic application restart during development. Simply rebuild your project in your IDE.

Future Enhancements

  • Spring Security integration (commented in pom.xml)
  • Authentication and authorization
  • User management
  • Snippet tagging and categorization
  • Full-text search capabilities
  • Snippet version history

Error Handling

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.

Health Check

A health check endpoint is available for monitoring:

GET /api/health

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

License

This project is provided as-is for educational and development purposes.

Support

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

About

A RESTful service for storing, retrieving, and managing code snippets with Spring Boot and PostgreSQL.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages