Refactor to idiomatic Spring + Kotlin code

- Usage of Kotlin JPA compiler plugin to generate default constructors
 - Jackson builder already register Kotlin module with sensible defaults
 - One liner syntax + type inference when possible
This commit is contained in:
Sebastien Deleuze 2017-06-16 22:36:18 +02:00
parent 5cf200dd0f
commit 5bbb84dce6
4 changed files with 11 additions and 28 deletions

View file

@ -5,15 +5,9 @@ import javax.persistence.AttributeConverter
class DoubleAttributeConverter : AttributeConverter<Double, BigDecimal?> {
override fun convertToDatabaseColumn(attribute: Double?): BigDecimal? {
return if (attribute != null) {
BigDecimal(attribute)
} else {
null
}
}
override fun convertToDatabaseColumn(attribute: Double?) =
if (attribute != null) { BigDecimal(attribute) } else { null }
override fun convertToEntityAttribute(dbData: BigDecimal?): Double? {
return dbData?.toDouble()
}
override fun convertToEntityAttribute(dbData: BigDecimal?) =
dbData?.toDouble()
}

View file

@ -21,13 +21,6 @@ internal data class CityEntity(
val updatedAt: LocalDateTime = LocalDateTime.now(),
val createdAt: LocalDateTime = LocalDateTime.now()) {
// Default constructor for JPA
@Suppress("unused")
private constructor() : this(
name = "",
location = Coordinate.origin(),
updatedAt = LocalDateTime.MIN)
fun toDto(): CityDto = CityDto(
id = this.id!!,
name = this.name,

View file

@ -23,7 +23,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.1.2-2</kotlin.version>
<kotlin.version>1.1.2-5</kotlin.version>
<mockito.version>2.8.9</mockito.version>
<assertj.version>3.8.0</assertj.version>
</properties>
@ -105,6 +105,7 @@
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
</compilerPlugins>
<jvmTarget>1.8</jvmTarget>
</configuration>
@ -130,6 +131,11 @@
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>

View file

@ -1,8 +1,5 @@
package de.rpr.mycity
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.InjectionPoint
@ -18,13 +15,6 @@ class Application {
@Scope("prototype")
fun logger(injectionPoint: InjectionPoint): Logger = LoggerFactory.getLogger(injectionPoint.methodParameter.containingClass)
@Bean
fun objectMapper(): ObjectMapper {
val mapper = ObjectMapper().registerModule(KotlinModule())
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
return mapper
}
}
fun main(args: Array<String>) {