kotlin-springboot
github/awesome-copilot
Best practices for building Spring Boot applications with Kotlin.
What is kotlin-springboot?
This skill provides guidance on writing high-quality, idiomatic Spring Boot applications using Kotlin. Use it when setting up new Kotlin/Spring Boot projects or refactoring existing ones to follow Kotlin conventions and Spring Boot patterns.
- Organize projects by feature/domain with proper package structure
- Use primary constructors for dependency injection with immutable val declarations
- Configure applications with type-safe data classes and externalized YAML configuration
- Design RESTful APIs with data class DTOs and global exception handling
- Leverage Kotlin null-safety in JPA entities and repositories
- Write idiomatic tests using JUnit 5, Kotest, and MockK
How to install kotlin-springboot
npx skills add https://github.com/github/awesome-copilot --skill kotlin-springboot- Maven or Gradle with Kotlin plugin configured
- Spring Boot 2.x or later
- JDK 11 or later
How to use kotlin-springboot
- 1.Organize code by feature/domain rather than by layer (e.g., com.example.app.order)
- 2.Use primary constructors with private val for all required dependencies
- 3.Define DTOs as data classes and entities with kotlin-jpa plugin enabled
- 4.Configure application properties in application.yml with @ConfigurationProperties data classes
- 5.Implement controllers with @RestController, services with @Service, and repositories extending JpaRepository
- 6.Add global exception handling with @ControllerAdvice and @ExceptionHandler
- 7.Use Kotest and MockK for idiomatic test assertions and mocking
- 8.Apply @Transactional at service method level for transaction management
Use cases
- Setting up a new Spring Boot microservice in Kotlin with proper DI patterns
- Refactoring a Java Spring Boot application to idiomatic Kotlin
- Implementing reactive endpoints using Kotlin coroutines with Spring Boot
- Creating type-safe configuration management with @ConfigurationProperties data classes
- Writing integration tests with Testcontainers for database-backed services
- Kotlin developers building Spring Boot applications
- Teams migrating from Java to Kotlin on Spring Boot
- Backend engineers implementing microservices with Spring Boot
kotlin-springboot FAQ
Always prefer val over var to promote immutability. Declare dependencies as private val in the primary constructor.
Enable the kotlin-jpa compiler plugin in your build configuration (pom.xml or build.gradle). It automatically makes entity classes open without boilerplate.
Use JUnit 5 as the default test framework, Kotest for fluent assertions, and MockK for Kotlin-native mocking. Both are designed specifically for Kotlin.
Never hardcode secrets. Use environment variables or dedicated secret management tools like HashiCorp Vault or AWS Secrets Manager.
Yes. Spring Boot has excellent support for Kotlin coroutines. Use suspend functions in controllers and services for non-blocking asynchronous code with structured concurrency.
Full instructions (SKILL.md)
Source of truth, from github/awesome-copilot.
name: kotlin-springboot description: 'Get best practices for developing applications with Spring Boot and Kotlin.'
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-pluginororg.jetbrains.kotlin.jvm). - Kotlin Plugins: For JPA, enable the
kotlin-jpaplugin to automatically make entity classesopenwithout 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 valin the primary constructor. Prefervalovervareverywhere to promote immutability. - Component Stereotypes: Use
@Service,@Repository, and@RestControllerannotations just as you would in Java.
Configuration
- Externalized Configuration: Use
application.ymlfor its readability and hierarchical structure. - Type-Safe Properties: Use
@ConfigurationPropertieswithdata classto 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 classfor all DTOs. This providesequals(),hashCode(),toString(), andcopy()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
@ControllerAdviceand@ExceptionHandlerfor consistent error responses.
Service Layer
- Business Logic: Encapsulate business logic within
@Serviceclasses. - Statelessness: Services should be stateless.
- Transaction Management: Use
@Transactionalon 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 thekotlin-jpacompiler 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
JpaRepositoryorCrudRepository. - 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
@WebMvcTestor@DataJpaTestto test specific parts of the application. - Testcontainers: Use Testcontainers for reliable integration tests with real databases, message brokers, etc.
Coroutines & Asynchronous Programming
suspendfunctions: For non-blocking asynchronous code, usesuspendfunctions in your controllers and services. Spring Boot has excellent support for coroutines.- Structured Concurrency: Use
coroutineScopeorsupervisorScopeto manage the lifecycle of coroutines.
Related skills
More from github/awesome-copilot and the wider catalog.
git-commit
Execute semantic git commits with conventional message analysis and intelligent staging.
excalidraw-diagram-generator
Generate Excalidraw diagrams from natural language descriptions.
documentation-writer
Create structured technical documentation using the Diátaxis framework for tutorials, how-to guides, references, and explanations.
gh-cli
GitHub CLI comprehensive reference for repositories, issues, PRs, Actions, projects, releases, and all GitHub operations from the command line.
prd
Generate comprehensive Product Requirements Documents with executive summaries, user stories, technical specs, and risk analysis.
refactor
Surgical code refactoring to improve maintainability without changing behavior.