I would like to serialize json data to LocalTime using kotlin and Jackson, but I cannot implement it.

Asked 2 years ago, Updated 2 years ago, 108 views

I'm a beginner at programming.
As the title says, I serialized data such as LocalTime using kotlin and Jackson.
I can't implement it properly.Please reply.

Write Code

JacksonPractice.kt
@RestController
@RequestMapping ("json")
class JacksonPractice {

    @ PostMapping(
            value = [ ] ,
            uses = ["application/json" ]
    )
    funpostJsonData(@RequestBodyjson:String){
        println("called getJsonData")
        println("-----json data-------")
        println(json)
        println("")
        val mapper = ObjectMapper()
        mapper.registerModule(JavaTimeModule())
        valjsonData=mapper.readValue<JsonData>(json)
        println("-----json serialize -----")
        println(jsonData)
    }
}

JsonDataModel.kt (Data Class)

package practice.kotlinjackson.model

import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.databind.announcement.JsonDeserialize
import com.fasterxml.jackson.databind.announcement.JsonSerialize
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer
import java.time.LocalDateTime
import java.time.LocalTime

data class JsonData(
        valid —Int,
        val name —String,
        @JsonSerialize(using=ToStringSerializer::class)
        @JsonDeserialize(using=LocalDateTimeDeser::class)
        value —LocalDateTime,
        @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="HH:mm:ss")
        valtime —LocalTime,
        valjsonDataDetail —JsonDataDetail
)

US>data class JsonDataDetail(
        val detail —String
)

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins{
    id(" org.springframework.boot") version "2.5.3"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.5.21"
    kotlin("plugin.spring") version "1.5.21"
}

group="practice.kotlin-jackson"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility=JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation(" org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("com.fasterxml.jackson.datatatype:jackson-datatype-jsr310:2.9.5")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation(" org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation(" org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<KotlinCompile>{
    US>kotlinOptions{
        freeCompilerArgs=listOf("-Xjsr305= strict")
        jvmTarget="11"
    }
}

tasks.withType<Test>{
    useJUnitPlatform()
}

As for the request, I have requested the following json data from postman

{
    "id"—10,
    "name": "kotlin jackson",
    "date": "2017-11-13T06:40:00Z",
    "time": "10:00:00",
    "jsonDataDetail": {
        "detail": "data detail"
    }
}

json spring spring-boot kotlin jackson

2022-09-30 11:28

2 Answers

KotlinModule registration is also required to handle Kotlin's data class directly:

funpostJsonData(@RequestBodyjson:String){
        println("called getJsonData")
        println("-----json data-------")
        println(json)
        println("")
        valmapper = ObjectMapper().registerModules (KotlinModule(), JavaTimeModule())
        valjsonData=mapper.readValue<JsonData>(json)
        println("-----json serialize -----")
        println(jsonData)
    }

It is also common to leave it to the message converter that is built into Spring Boot by default:

funpostJsonData(@RequestBodyjsonData:JsonData){
        println("-----json serialize -----")
        println(jsonData)
    }


2022-09-30 11:28

Write down the code that was finally resolved for the memorandum
https://github.com/ShutoKeisuke/kotlin-jackson-practice

Controller

@RestController
@RequestMapping ("json")
class JacksonPractice {

    @ PostMapping(
            value = [ ] ,
            uses = ["application/json" ]
    )
    funpostJsonData(@RequestBodyjson:String){
        println("called getJsonData")
        println("-----json data-------")
        println(json)
        println("")
        valmapper = ObjectMapper().registerKotlinModule()
        mapper.registerModule(JavaTimeModule())
        valjsonData=mapper.readValue<JsonData>(json)
        println("-----json serialize -----")
        println(jsonData)
    }

    @ PostMapping (value=["converter"])
    funpostMessageConverter(@RequestBodyjsonData:JsonData){
        println("-----json serialize -----")
        println(jsonData)
    }
}

Data Class

import java.time.LocalDateTime
import java.time.LocalTime

data class JsonData(
        valid —Int,
        val name —String,
//        @JsonSerialize(using=ToStringSerializer::class)
//        @JsonDeserialize(using=LocalDateTimeDeser::class)
        value —LocalDateTime,
//        @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="HH:mm")
        valtime —LocalTime,
        valjsonDataDetail —JsonDataDetail
)

US>data class JsonDataDetail(
        val detail —String
)

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins{
    id(" org.springframework.boot") version "2.5.3"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.5.21"
    kotlin("plugin.spring") version "1.5.21"
}

group="practice.kotlin-jackson"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility=JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation(" org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("com.fasterxml.jackson.datatatype:jackson-datatype-jsr310:2.9.5")
    // ↓I probably don't need it
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.+")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation(" org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation(" org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<KotlinCompile>{
    US>kotlinOptions{
        freeCompilerArgs=listOf("-Xjsr305= strict")
        jvmTarget="11"
    }
}

tasks.withType<Test>{
    useJUnitPlatform()
}

Json *Request from Postman to confirm

{
    "id"—10,
    "name": "kotlin jackson",
    "date": "2017-11-13T06:40:00Z",
    "time": "10:00",
    "jsonDataDetail": {
        "detail": "data detail"
    }
}


2022-09-30 11:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.