Contrôleur Spring MVC, Spring Boot en Kotlin
Utiliser le Spring Initializr sur https://start.spring.io/ pour générer une application Maven Kotlin :
Item |
valeur |
Group |
fr.eni.kotlin.spring5.mvc |
Artefact |
kotlin |
Name |
kotlin |
Description |
Exemple Spring 5 Kotlin Spring Boot Spring MVC |
Package Name |
fr.eni.kotlin.spring5.mvc.app |
Packaging |
jar |
Java Version |
17 |
Spring Boot |
2.6.7 |
Table 3. Paramètres.
Nom des dépendances |
Spring Web |
Mustache |
Spring Data JPA |
H2 Database |
Spring Boot DevTools |
Rest Repository HAL Browser |
Table 4. Dépendances.
Nous avons les dépendances :
Librairie |
Utilité |
kotlin-stdlib-jdk8 |
Variante Java 8 de la bibliothèque standard Kotlin. |
kotlin-reflect |
La bibliothèque de réflexion Kotlin. |
jackson-module-kotlin |
Prise en charge de la sérialisation/désérialisation des classes. |
1. Fonction principale
Nous avons la classe pour lancer notre programme :
KotlinApplication :
@SpringBootApplication
class KotlinApplication
fun main(args: Array<String>) {
runApplication<KotlinApplication>(*args)
}
2. Test associé à la fonction principale
Voici un test de la classe du programme principal.
KotlinApplicationTests.kt :
@SpringBootTest
class KotlinApplicationTests {
@Test
fun contextLoads() {
}
}
Il faut configurer la base H2 dans le fichier application.properties :
spring.datasource.url = jdbc:h2:~/test...