Input validation is crucial for building secure and robust REST APIs. In this article, we will explore how to implement input validation in Spring Boot using Java, Kotlin, and Go (with Gin).
Written by
Şuayb Şimşek
Backend-focused fullstack developer sharing practical notes on Spring Boot, security, microservices, and cloud-native architecture.
Get practical backend + fullstack notes when new articles are published.
Social
🌟 Why Validate Input?
Validation ensures the data sent to your API adheres to expected formats and prevents potential vulnerabilities like SQL Injection, XSS, and bad data entries.
📋 Prerequisites
Before implementing validation, make sure you have:
Java 17+ for Spring Boot examples
Go 1.21+ for Gin examples
A running Spring Boot or Gin starter project
Basic familiarity with DTOs, JSON payloads, and HTTP status codes
🧪 Step 1: Add Validation Dependencies
For Spring Boot projects, include the following dependencies:
# Install the Gin framework
go get -u github.com/gin-gonic/gin
# Install the validator package
go get -u github.com/go-playground/validator/v10
🧪 Step 2: Define a DTO with Validation Rules
Use annotations to define validation constraints on fields. Examples include @NotNull, @Size, and @Pattern.
🧪 Step 3: Create a Controller with Validation
Integrate validation into your REST endpoints.
🧪 Step 4: Handle Validation Errors
Customize error handling to return user-friendly responses.
main.go Example
Here is an example of the main.go file for setting up a Gin application:
GOmain.go
package main
import (
"controller"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.POST("/api/todos", controller.CreateTodoHandler)
r.Run() // Start the server on http://localhost:8080
}
▶️ Step 5: Run the Application
To run the application:
Spring Boot (Java/Kotlin):
Run the Spring Boot application from your IDE or terminal:
BASH
./mvnw spring-boot:run # For Maven projects
./gradlew bootRun # For Gradle projects
Access the API at http://localhost:8080/api/todos.
Gin (Go)
Run the Go application:
BASH
go run main.go
Access the API at http://localhost:8080/api/todos.
🧪 Testing with cURL
Here are some example cURL commands to test the API:
POST a new Todo:
BASH
curl -X POST http://localhost:8080/api/todos \
-H "Content-Type: application/json" \
-d '{"title": "New Task", "completed": false}'
You now have a practical Input Validation in REST APIs implementation with a clear, production-friendly Spring Boot structure. As a next step, adapt configuration and tests to your own domain, then validate behavior under realistic traffic and failure scenarios.
JAVATodoRequest.java
package com.example.demo.dto;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
@Data
public class TodoRequest {
@NotNull(message = "Title is required")
@Size(min = 3, max = 50, message = "Title must be between 3 and 50 characters")
private String title;
private boolean completed;
}
KOTLINTodoRequest.kt
package com.example.demo.dto
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
data class TodoRequest(
@field:NotNull(message = "Title is required")
@field:Size(min = 3, max = 50, message = "Title must be between 3 and 50 characters")
val title: String?,
val completed: Boolean = false
)
GOapp.go
package dto
import (
"github.com/go-playground/validator/v10"
)
type TodoRequest struct {
Title string `validate:"required,min=3,max=50"`
Completed bool `validate:""`
}
var validate = validator.New()
func ValidateTodoRequest(todo TodoRequest) error {
return validate.Struct(todo)
}
JAVATodoController.java
package com.example.demo.controller;
import com.example.demo.dto.TodoRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/todos")
public class TodoController {
@PostMapping
public String createTodo(@Validated @RequestBody TodoRequest request) {
return "Todo created: " + request.getTitle();
}
}