From 5bbb84dce6618f0c0885991cbf32ba21169a7f0e Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Fri, 16 Jun 2017 22:36:18 +0200 Subject: [PATCH] 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 --- .../rpr/mycity/domain/DoubleAttributeConverter.kt | 14 ++++---------- .../de/rpr/mycity/domain/city/entity/CityEntity.kt | 7 ------- pom.xml | 8 +++++++- web/src/main/kotlin/de/rpr/mycity/Application.kt | 10 ---------- 4 files changed, 11 insertions(+), 28 deletions(-) diff --git a/domain/src/main/kotlin/de/rpr/mycity/domain/DoubleAttributeConverter.kt b/domain/src/main/kotlin/de/rpr/mycity/domain/DoubleAttributeConverter.kt index f5c6789..49e6f50 100644 --- a/domain/src/main/kotlin/de/rpr/mycity/domain/DoubleAttributeConverter.kt +++ b/domain/src/main/kotlin/de/rpr/mycity/domain/DoubleAttributeConverter.kt @@ -5,15 +5,9 @@ import javax.persistence.AttributeConverter class DoubleAttributeConverter : AttributeConverter { - 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() } \ No newline at end of file diff --git a/domain/src/main/kotlin/de/rpr/mycity/domain/city/entity/CityEntity.kt b/domain/src/main/kotlin/de/rpr/mycity/domain/city/entity/CityEntity.kt index 92462a5..6807a77 100644 --- a/domain/src/main/kotlin/de/rpr/mycity/domain/city/entity/CityEntity.kt +++ b/domain/src/main/kotlin/de/rpr/mycity/domain/city/entity/CityEntity.kt @@ -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, diff --git a/pom.xml b/pom.xml index e8d13b7..c9d4884 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ UTF-8 UTF-8 1.8 - 1.1.2-2 + 1.1.2-5 2.8.9 3.8.0 @@ -105,6 +105,7 @@ spring + jpa 1.8 @@ -130,6 +131,11 @@ kotlin-maven-allopen ${kotlin.version} + + org.jetbrains.kotlin + kotlin-maven-noarg + ${kotlin.version} + diff --git a/web/src/main/kotlin/de/rpr/mycity/Application.kt b/web/src/main/kotlin/de/rpr/mycity/Application.kt index 5e4774c..cb0134c 100644 --- a/web/src/main/kotlin/de/rpr/mycity/Application.kt +++ b/web/src/main/kotlin/de/rpr/mycity/Application.kt @@ -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) {