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"
}
}
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)
}
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"
}
}
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
578 Understanding How to Configure Google API Key
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.