[Memo] JSUG Study Group 2020 Part 1 Spring x Kotlin

JSUG Study Group 2020 Part 1 Spring x Kotlin Share the notes you took when you attended.

It's very simple, but I hope you find it helpful.

Spring Complete Understanding Guide in Kotlin (@ngsw_taro)

With XX you can easily start Spring development with Kotlin

Spring Initializr Intellij IDEA (paid version)

Bean registration and handler definition using annotations in Kotlin

Kotlin does not inherit by default, so you need to add the open modifier to the service class. However, if you use Kotlin's allopen plugin or kotlin-spring plugin, it will be treated as open automatically.

By the way, it is set from the beginning in the project generated by Spring Initializr.

Be careful of validation. In the sense of Java

class Body {
	@NotNull val value: Int
}

If you write, validation will not be applied.

class Body {
	@field:NotNull val value: Int?
}

Write like this.

Relatively new bean registration and handler definition using DSL for Kotlin

Bean Definition DSL Router DSL

Linter ktlint so-called Linter and Formatter There is a Gradle plugin. There is also an automatic setting for IntelliJ IDEA.

Convenient Kotlin coroutines with WebFlux

Kotlin has coroutines. Reactor compatible libraries are official.

Ktor feels good

・ JetBrains official ・ Very thin, extra functions are left to the 3rd party ・ DSL ・ Non-blocking

Testing with Junit in Kotlin

Use the extension function expectBody for Kotlin. The assertion library uses AssertJ. (Of course Kotlin compatible) I like the feeling of writing in the method chain.

Also, I'm interested in assertk.

If you want to mock with Kotlin, MockK is a good feeling.

Also supports coroutines.

Bonus: How do you access DB?

I'm using a JDBC template. I'm writing raw SQL and steadily mapping it to a class. The exposed is also made by JetBrains and looks good.

Kotlin support in Spring 5.2 (@gumimin)

Starting with Spring 5.2, Kotlin code has also been added to the official Spring documentation.

Router Functions A new routing method that is different from the traditional annotation base WebFlux.fn → From Spring 5 WebMVC.fn → From Spring 5.2 Functional routing method

MockMvc DSL Cleaner and easier to understand than previous method chains

@ConfigurationProperties Binding to multiple properties at once Relaxed Binding works

From Boot 2.2.1, you can get the setting value with immutable through the constructor with @ConstructorBinding. You now need @Compornent and @EnableAutoConfiguration.

Coroutine support

What is a coroutine? -Computation instance that makes it easy to suspend / resume processing ・ Something like a weighing thread ・ Can be described in async / await style ・ The concept of scope makes it easy to manage the dependencies between coroutines. -A conversion module to data types compliant with other Reactive Streams is provided.

WebFlux's support for coroutines has made it possible to use coroutine types instead of reactors, making it more Kotlin-like and easier to parallelize.

Performance is much better if you can program Reactive.

bonus

The State of Kotlin Support in Spring is a recommended video

Impressions

I'm glad I learned a lot about the notes and tips when writing Spring Boot in Kotlin. I will start with Spring Initializr as well.

Also, since the word about Web Flux was at a level I had heard, I understood the concept a little more and decided to write it because it can actually be Java.

Once again, thank you very much to all the speakers and operators.

Recommended Posts

[Memo] JSUG Study Group 2020 Part 1 Spring x Kotlin
[Memo] JSUG Study Group 2019 Part 7 Utilization of Spring in Visional
JSUG Study Group 2018 Part 4 Spring 5 & Spring Boot 2 Hands-on Impressions
Memo of JSUG Study Group 2018 Part 2-Efforts for working specifications in the Spring and API era-
JSUG Study Group Participation Report
spring framework Simple study memo (2): AOP
Memo: Kotlin + Room + RecyclerView + DataBinding (Part 2)
Spring Framework study notes [Part 1] DI container
spring framework Simple study memo (1): ApplicationContext, Bean, Autowired
Spring retrospective memo
Kotlin Study Book (Kotlin)
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~