Recently, I encountered sql state 42000 error in a spring boot application while moving from MySQL version 5.6 to 8.0. After inspecting stack trace and some googling, I got to know that few default configurations have changed in MySQL 8.0 (as expected 😃 ).
This caused our poorly written queries to fail during migration.
Background # Lets look at sample query using spring-data jpa to gather some analytics by aggregating data about a customer ordering products
Let’s have a look at enums in kotlin. Unlike Java, where enum is a Type; in Kotlin, enums are classes
Defining an enum # enum class Direction { NORTH, SOUTH, WEST, EAST } Defining an enum with a variable # enum class Direction(val shortName: Char) { NORTH('N'), SOUTH('S'), WEST('W'), EAST('E') } Template Method pattern in Enum # enum class Direction(val shortName: Char) { NORTH('N') { override fun move() = "Moving in North Direction" }, SOUTH('S') { override fun move() = "Moving in South Direction" }, WEST('W'){ override fun move() = "Moving in West Direction" }, EAST('E'){ override fun move() = "Moving in East Direction" }; abstract fun move(): String } Companion methods in enum # enum class Direction(val shortName: String) { NORTH("N"), SOUTH("S"), WEST("W"), EAST("E"); companion object { fun valueOfIgnoreCase(name: String) = valueOf(name.uppercase(Locale.getDefault())) fun valueOfShortName(shortName: String): Direction? = Direction.values().find { it.shortName == shortName } } } Links # https://kotlinlang.org/docs/enum-classes.html https://www.baeldung.com/kotlin/enum
When working as a backend engineer, it is essential to document the REST APIs. It also helps in providing a UI(swagger-ui) to test the REST calls. Let us try to integrate springdoc-openapi to provide swagger documentation for a spring boot project using spring-security(OAuth2).
Add the dependencies to build.gradle # implementation("org.springdoc:springdoc-openapi-ui:1.6.8") implementation("org.springdoc:springdoc-openapi-security:1.6.8") implementation("org.springdoc:springdoc-openapi-kotlin:1.6.8") Kotlin code example # The following example showcases spring configuration to create OpenAPI bean which supports two authentication mechanism:
Error # While trying to setup Azure Cache for Redis as spring session store, I encountered following exception: .JedisDataException: ERR unknown command CONFIG, with args beginning with: get, notify-keyspace-events": 1,
Fix # Found the solution in spring-session-gh-issue
Kotlin @Configuration class HttpSessionConfig() { companion object { // another solution could be to enable notify-keyspace-events for redis @Bean fun configureRedisAction(): ConfigureRedisAction { return ConfigureRedisAction.NO_OP } } } Java @Configuration class HttpSessionConfig() { // another solution could be to enable notify-keyspace-events for redis @Bean public static ConfigureRedisAction configureRedisAction() { return ConfigureRedisAction.NO_OP; } } Links # spring-session-gh-issue