The State of Kotlin Support in Spring Sbastien Deleuze @sdeleuze - - PowerPoint PPT Presentation

the state of kotlin support in spring s bastien deleuze
SMART_READER_LITE
LIVE PREVIEW

The State of Kotlin Support in Spring Sbastien Deleuze @sdeleuze - - PowerPoint PPT Presentation

Kotlin 1.4 Online Event The State of Kotlin Support in Spring Sbastien Deleuze @sdeleuze October 15, 2020 Spring is the server-side leader on the JVM Source: a Picture of Java in 2020, JetBrains First class support for Java and Kotlin


slide-1
SLIDE 1

Kotlin 1.4 Online Event October 15, 2020 @sdeleuze

The State of Kotlin Support in Spring Sébastien Deleuze

slide-2
SLIDE 2

Spring is the server-side leader on the JVM

Source: a Picture of Java in 2020, JetBrains

slide-3
SLIDE 3

First class support for Java and Kotlin

slide-4
SLIDE 4

More Spring Boot projects generated with Kotlin each year.

slide-5
SLIDE 5

Let’s focus on Kotlin today

slide-6
SLIDE 6

Getting started

slide-7
SLIDE 7

Start your project on https://start.spring.io

slide-8
SLIDE 8

Minimal Spring Boot Kotlin application

slide-9
SLIDE 9

Gradle Kotlin DSL

slide-10
SLIDE 10

Also available in IDEs

IntelliJ IDEA Ultimate VS code

slide-11
SLIDE 11

Follow the tutorial on https://spring.io/guides

slide-12
SLIDE 12

Spring Framework documentation in Kotlin

slide-13
SLIDE 13

Choose your style

slide-14
SLIDE 14

Choose your web server stack

Do I need asynchronous

  • r reactive

programming? Spring MVC Tomcat Spring WebFlux with Coroutines Netty No Yes Persistence Spring Data JPA JDBC Redis Mongo ... Spring Data Reactive with Coroutines R2DBC Redis Mongo ... Persistence

slide-15
SLIDE 15

Choose your programming model

slide-16
SLIDE 16

Do you prefer annotations?

@RestController @RequestMapping("/api/article") class ArticleController(private val repository: ArticleRepository) { @GetMapping("/") fun findAll() = repository.findAllByOrderByAddedAtDesc() @GetMapping("/{slug}") fun findOne(@PathVariable slug: String) = repository.findBySlug(slug) ?: throw ResponseStatusException(NOT_FOUND) }

slide-17
SLIDE 17

Or functional APIs?

@Bean fun route(repository: ArticleRepository) = router { "/api/article".nest { GET("/") {

  • k().body(repository.findAllByOrderByAddedAtDesc())

} GET("/{slug}") { val slug = it.pathVariable("slug") val article = repository.findBySlug(slug) ?: throw ResponseStatusException(NOT_FOUND)

  • k().body(article)

} } }

slide-18
SLIDE 18

Spring supports both, so up to you.

slide-19
SLIDE 19

Coroutines

slide-20
SLIDE 20

Allow to go reactive with a great trade-off between imperative and functional programming.

slide-21
SLIDE 21

Coroutines are the default way to go reactive in Spring with Kotlin.

slide-22
SLIDE 22

First class Coroutines support

  • Spring WebFlux
  • Spring MVC (new in Spring Boot 2.4
  • Spring Data Reactive
  • Spring Messaging (RSocket)
  • Spring Vault
slide-23
SLIDE 23

Suspending functions

Spring MVC and WebFlux

@GetMapping("/api/banner") suspend fun suspendingEndpoint(): Banner { delay(10) return Banner("title", "Lorem ipsum") }

slide-24
SLIDE 24

Flow

Spring MVC and WebFlux

@GetMapping("/banners") suspend fun flow(): Flow<Banner> = client.get() .uri("/messages") .accept(MediaType.TEXT_EVENT_STREAM) .retrieve() .bodyToFlow<String>() .map { Banner("title", it) }

slide-25
SLIDE 25

Multiplatform

slide-26
SLIDE 26

Multiplatform

slide-27
SLIDE 27

kotlinx.serialization

slide-28
SLIDE 28

kotlinx.serialization support

New in Spring Boot 2.4

  • More lightweight than Jackson
  • Designed for Kotlin
  • Multiplatform serialization
  • Allows same code for model and validation

across server, frontend and mobile!

implementation("org.springframework.boot:spring-boot-starter-web") { exclude(module = "spring-boot-starter-json") } implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")

slide-29
SLIDE 29

Kotlin/JS

slide-30
SLIDE 30

Kotlin/WASM has a huge potential

slide-31
SLIDE 31

RSocket

slide-32
SLIDE 32

RSocket

Reactive Streams

RSocket

Reactive Streams Reactive Streams

slide-33
SLIDE 33

RSocket

.Python .NET Go C++ JavaScript

Reactive Streams

RSocket

Kotlin Java

Reactor

slide-34
SLIDE 34

RSocket support in Spring messaging

class MessageHandler(private val builder: RSocketRequester.Builder) { // ... suspend fun stream(request: ServerRequest): ServerResponse { val requester = builder .dataMimeType(APPLICATION_CBOR) .connectTcpAndAwait("localhost", 9898) val replies = requester .route("bot.messages") .dataWithType(processor) .retrieveFlow<Message>() val broadcast = requester.route("bot.broadcast").retrieveFlow<Message>() val messages = flowOf(replies, processor.asFlow(), broadcast).flattenMerge() return ok().sse().bodyAndAwait(messages) } }

slide-35
SLIDE 35

rsocket-kotlin

slide-36
SLIDE 36

Other key points

slide-37
SLIDE 37

100% of Spring Framework API with null-safety annotations → no NPE for Spring applications written in Kotlin

slide-38
SLIDE 38

ConfigurationProperties data classes

@ConstructorBinding @ConfigurationProperties("blog") data class BlogProperties(val title: String, val banner: Banner) { data class Banner(val title: String? = null, val content: String) }

slide-39
SLIDE 39

Spring Security Kotlin DSL

New in Spring Security 5.4

  • verride fun configure(http: HttpSecurity) {

http { authorizeRequests { authorize("/css/**", permitAll) authorize("/user/**", hasAuthority("ROLE_USER")) } formLogin { loginPage = "/log-in" } } }

slide-40
SLIDE 40

Spring Boot native applications

With GraalVM native

slide-41
SLIDE 41

Spring Boot application

EXE

Native executable Lightweight container image

slide-42
SLIDE 42

JVM and native executables offer different trade-offs

slide-43
SLIDE 43

Instant startup and cheaper hosting

Spring Boot on Native, 1 vCPU, 256M RAM Spring Boot on JVM, 4 vCPU, 1G RAM

slide-44
SLIDE 44

Spring support for native executables

GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin (Spring Boot 2 baseline) First class support in Spring Boot 3 Today Dec 2020 Beta Spring Boot 3 release

slide-45
SLIDE 45

Demo

slide-46
SLIDE 46

What’s next?

Programmatic configuration for Spring Boot using a Kotlin DSL

slide-47
SLIDE 47

Spring Fu is an incubator for a functional flavor of Spring Boot

KoFu JaFu

slide-48
SLIDE 48

What is the same than Spring Boot?

  • https://start.spring.io
  • Based on Spring Boot infrastructure
  • Spring configuration for the JVM ecosystem
  • Dependency management
  • Starters
  • Actuators
  • Standalone executable JAR or container deployment
slide-49
SLIDE 49

What changes?

Conventions and automatic configuration Annotations-based configuration Reflection-based infrastructure Production ready Explicit declaration Functional configuration Lambda-based infrastructure Incubating

Spring Boot regular flavor Spring Fu flavor

slide-50
SLIDE 50

Spring Boot configured with KoFu

val app = webApplication { beans { bean<SampleService>() bean<SampleHandler>() } webMvc { port = if (profiles.contains("test")) 8181 else 8080 router { val handler = ref<SampleHandler>() GET("/", handler::hello) GET("/api", handler::json) } converters { string() jackson { indentOutput = true } } } } fun main() { app.run() }

slide-51
SLIDE 51

Links

https://start.spring.io https://spring.io/guides/tutorials/spring-boot-kotlin/ https://github.com/spring-projects-experimental/spring-graalvm-native https://github.com/spring-projects-experimental/spring-fu

slide-52
SLIDE 52

@sdeleuze

Thanks! Have a nice Kotlin!