Skip to content

mathalama/contact-form-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contact Form API

A modern, production-ready REST API built with Spring Boot for seamless contact form submission handling.
Designed for reliability, validation, and scalability in real-world backend systems.

Java Spring Boot Docker Maven


Features

  • RESTful API design
  • Input validation using Jakarta Validation
  • Layered architecture (Controller → Service → Repository)
  • JPA integration (MySQL / H2)
  • Docker & Docker Compose support
  • Environment-based configuration (dev / prod)
  • Logging & structured error handling
  • Production-ready security configuration

Tech Stack

  • Java 17+
  • Spring Boot
  • Spring Data JPA
  • MySQL / H2
  • Lombok
  • JUnit 5 & Mockito
  • Docker

Getting Started

Build the Project

./mvnw clean package

Run the Application

./mvnw spring-boot:run

The API will be available at:

http://localhost:8080

Docker Deployment

Build Docker Image

docker build -t contact-form-api .

Run Container

docker run -p 8080:8080 contact-form-api

Docker Compose (Optional)

Create a docker-compose.yml file:

version: '3.8'

services:
  contact-form-api:
    build: .
    ports:
      - "8080:8080"
    environment:
      - SPRING_PROFILES_ACTIVE=prod
    restart: unless-stopped

Run:

docker-compose up -d

API Endpoints

Submit Contact Form

POST /api/contact
Content-Type: application/json

Request Body

{
  "name": "John Doe",
  "email": "john@example.com",
  "subject": "Inquiry",
  "message": "Your message here"
}

Success Response (200 OK)

{
  "id": "123",
  "status": "received",
  "timestamp": "2026-03-01T10:30:00Z"
}

Error Response (400 Bad Request)

{
  "status": 400,
  "message": "Validation failed",
  "errors": [
    {
      "field": "email",
      "message": "Invalid email format"
    }
  ]
}

Get Contact by ID

GET /api/contact/{id}

Response

{
  "id": "123",
  "name": "John Doe",
  "email": "john@example.com",
  "subject": "Inquiry",
  "message": "Your message here",
  "status": "received",
  "createdAt": "2026-03-01T10:30:00Z"
}

List Contacts (Paginated)

GET /api/contact?page=0&size=10

Response

{
  "content": [
    {
      "id": "123",
      "name": "John Doe",
      "email": "john@example.com",
      "subject": "Inquiry",
      "status": "received",
      "createdAt": "2026-03-01T10:30:00Z"
    }
  ],
  "totalElements": 1,
  "totalPages": 1,
  "currentPage": 0
}

Project Structure

contact-form-api/
├── src/
│   ├── main/
│   │   ├── java/com/mathalama/contactform/
│   │   │   ├── controller/
│   │   │   ├── service/
│   │   │   ├── model/
│   │   │   ├── repository/
│   │   │   ├── exception/
│   │   │   └── ContactFormApiApplication.java
│   │   └── resources/
│   │       ├── application.properties
│   │       ├── application-dev.properties
│   │       └── application-prod.properties
│   └── test/
├── pom.xml
├── Dockerfile
└── README.md

Configuration

Default (application.properties)

Includes:

  • Server configuration
  • Database connection
  • Logging configuration
  • Jackson settings
  • CORS settings

Profiles:

  • dev -- verbose logging & SQL output
  • prod -- optimized for production (SSL enabled)

Run with profile:

./mvnw spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"

Development

Run Tests

./mvnw test

Code Quality

./mvnw spotless:check
./mvnw spotless:apply

Generate Javadoc

./mvnw javadoc:javadoc

Create Executable JAR

./mvnw clean package -DskipTests
java -jar target/contact-form-api-1.0.0.jar

Security Best Practices

  • Input validation on all endpoints
  • Parameterized queries (SQL injection prevention)
  • CORS configuration
  • HTTPS-ready production profile
  • Environment-based secrets
  • CSRF protection
  • Rate limiting support

Example Security Configuration:

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .cors()
            .and()
            .authorizeRequests()
            .antMatchers("/api/contact").permitAll()
            .anyRequest().authenticated();
        return http.build();
    }
}

Roadmap

  • Email notification integration
  • Full JPA persistence layer
  • Admin dashboard
  • Analytics & reporting
  • Multi-language support
  • WebSocket real-time updates
  • OAuth2 authentication
  • Scheduled batch processing
  • Export (PDF / CSV)
  • Automated response templates

Contributing

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature/amazing-feature
  3. Commit changes
    git commit -m "Add amazing feature"
  4. Push to branch
    git push origin feature/amazing-feature
  5. Open a Pull Request

Ensure:

  • Tests pass
  • Code style is consistent
  • Documentation is updated

License

MIT License. See LICENSE for details.


Built with precision by mathalama

About

Contact Form API - A Spring Boot REST API for processing and managing contact form submissions with Docker support.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors