Skip to content

Commit

Permalink
feat: LocalDateTime 응답 마지막에 Z 추가 (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeryboy authored Sep 17, 2023
1 parent e744b21 commit d1425e8
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.wafflestudio.csereal.common.config

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.module.SimpleModule
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

@Configuration
class JacksonConfig {

@Bean
fun objectMapper(): ObjectMapper {
val objectMapper = ObjectMapper()
val module = SimpleModule()
module.addSerializer(LocalDateTime::class.java, LocalDateTimeSerializer())
objectMapper.registerModule(module)
return objectMapper
}
}

class LocalDateTimeSerializer : JsonSerializer<LocalDateTime>() {
override fun serialize(value: LocalDateTime, gen: JsonGenerator, serializers: SerializerProvider) {
val zonedDateTime = value.atZone(ZoneOffset.UTC)
val formatted = zonedDateTime.format(DateTimeFormatter.ISO_INSTANT)
gen.writeString(formatted)
}
}

0 comments on commit d1425e8

Please sign in to comment.