Catalog
github/kotlin-springboot

github

kotlin-springboot

Get best practices for developing applications with Spring Boot and Kotlin.

v1.0Latest
New~1.1kUpdated Jun 26, 2026

Spring Boot with Kotlin Best Practices

Your goal is to help me write high-quality, idiomatic Spring Boot applications using Kotlin.

Project Setup & Structure

  • Build Tool: Use Maven (pom.xml) or Gradle (build.gradle) with the Kotlin plugins (kotlin-maven-plugin or org.jetbrains.kotlin.jvm).
  • Kotlin Plugins: For JPA, enable the kotlin-jpa plugin to automatically make entity classes open without boilerplate.
  • Starters: Use Spring Boot starters (e.g., spring-boot-starter-web, spring-boot-starter-data-jpa) as usual.
  • Package Structure: Organize code by feature/domain (e.g., com.example.app.order, com.example.app.user) rather than by layer.

Dependency Injection & Components

  • Primary Constructors: Always use the primary constructor for required dependency injection. It's the most idiomatic and concise approach in Kotlin.
  • Immutability: Declare dependencies as private val in the primary constructor. Prefer val over var everywhere to promote immutability.
  • Component Stereotypes: Use @Service, @Repository, and @RestController annotations just as you would in Java.

Configuration

  • Externalized Configuration: Use application.yml for its readability and hierarchical structure.
  • Type-Safe Properties: Use @ConfigurationProperties with data class to create immutable, type-safe configuration objects.
  • Profiles: Use Spring Profiles (application-dev.yml, application-prod.yml) to manage environment-specific configurations.
  • Secrets Management: Never hardcode secrets. Use environment variables or a dedicated secret management tool like HashiCorp Vault or AWS Secrets Manager.

Web Layer (Controllers)

  • RESTful APIs: Design clear and consistent RESTful endpoints.
  • Data Classes for DTOs: Use Kotlin data class for all DTOs. This provides equals(), hashCode(), toString(), and copy() for free and promotes immutability.
  • Validation: Use Java Bean Validation (JSR 380) with annotations (@Valid, @NotNull, @Size) on your DTO data classes.
  • Error Handling: Implement a global exception handler using @ControllerAdvice and @ExceptionHandler for consistent error responses.

Service Layer

  • Business Logic: Encapsulate business logic within @Service classes.
  • Statelessness: Services should be stateless.
  • Transaction Management: Use @Transactional on service methods. In Kotlin, this can be applied to class or function level.

Data Layer (Repositories)

  • JPA Entities: Define entities as classes. Remember they must be open. It's highly recommended to use the kotlin-jpa compiler plugin to handle this automatically.
  • Null Safety: Leverage Kotlin's null-safety (?) to clearly define which entity fields are optional or required at the type level.
  • Spring Data JPA: Use Spring Data JPA repositories by extending JpaRepository or CrudRepository.
  • Coroutines: For reactive applications, leverage Spring Boot's support for Kotlin Coroutines in the data layer.

Logging

  • Companion Object Logger: The idiomatic way to declare a logger is in a companion object.
    companion object {
        private val logger = LoggerFactory.getLogger(MyClass::class.java)
    }
    
  • Parameterized Logging: Use parameterized messages (logger.info("Processing user {}...", userId)) for performance and clarity.

Testing

  • JUnit 5: JUnit 5 is the default and works seamlessly with Kotlin.
  • Idiomatic Testing Libraries: For more fluent and idiomatic tests, consider using Kotest for assertions and MockK for mocking. They are designed for Kotlin and offer a more expressive syntax.
  • Test Slices: Use test slice annotations like @WebMvcTest or @DataJpaTest to test specific parts of the application.
  • Testcontainers: Use Testcontainers for reliable integration tests with real databases, message brokers, etc.

Coroutines & Asynchronous Programming

  • suspend functions: For non-blocking asynchronous code, use suspend functions in your controllers and services. Spring Boot has excellent support for coroutines.
  • Structured Concurrency: Use coroutineScope or supervisorScope to manage the lifecycle of coroutines.
Files1
1 files · 1.0 KB

Select a file to preview

Overall Score

86/100

Grade

A

Excellent

Safety

92

Quality

85

Clarity

88

Completeness

78

Summary

This skill provides comprehensive best practices for developing Spring Boot applications with Kotlin, covering architecture, dependency injection, configuration, web layer design, testing, and asynchronous programming. It guides developers toward idiomatic Kotlin patterns (primary constructors, data classes, immutability) and Spring Boot conventions without performing any automated file modifications or executing commands.

Detected Capabilities

code examplearchitecture guidancebest practices documentationdesign pattern instruction

Trigger Keywords

Phrases that MCP clients use to match this skill to user intent.

spring boot kotlin setupkotlin service layerspring data jpa kotlinkotlin dto validationkotlin rest controllercoroutines spring bootkotest mockk testing

Use Cases

  • Set up a new Spring Boot + Kotlin project with proper structure and dependencies
  • Write idiomatic Kotlin service and repository classes with proper dependency injection
  • Design type-safe configuration objects using Kotlin data classes
  • Implement RESTful controllers with DTOs and validation
  • Write fluent, Kotlin-idiomatic tests using Kotest and MockK
  • Implement asynchronous code using Kotlin Coroutines in Spring Boot

Quality Notes

  • Clear hierarchical structure with descriptive section headings (Project Setup, Dependency Injection, Configuration, Web Layer, etc.)
  • Excellent security guidance: explicitly recommends against hardcoding secrets and suggests proper alternatives (environment variables, Vault, AWS Secrets Manager)
  • Concrete code examples provided (e.g., companion object logger pattern) that agents can follow directly
  • Well-defined use cases across the full application stack (setup, services, controllers, repositories, testing, async)
  • Practical recommendations grounded in Kotlin idioms (data classes for immutability, primary constructors, null-safety)
  • Covers testing best practices with specific library recommendations (JUnit 5, Kotest, MockK, Testcontainers)
  • No vague or open-ended instructions — each section provides actionable guidance
  • Addresses non-obvious Kotlin integration points (kotlin-jpa plugin, coroutine support, null safety for entities)
Model: claude-haiku-4-5-20251001Analyzed: Jun 26, 2026

Reviews

Add this skill to your library to leave a review.

No reviews yet

Be the first to share your experience.

Use github/kotlin-springboot in your dev environment

Command Palette

Search for a command to run...