Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin ObjectMapper사용시 런타임에 kotlin no creators, like default constructor, exist 로그가 출력되며 실행되지 않을경우 #1

Open
saechimdaeki opened this issue Mar 21, 2023 · 0 comments

Comments

@saechimdaeki
Copy link
Owner

data class CheckOutDto(
    var checkOutId: Long?,
    val memberId: Long,
    val productId: Long,
    val amount : Long,
    val shippingAddress : String,
    var createdAt : LocalDate?
)

다음과 같은 dataClass가 있다고 했을 때에 JsonString으로 들어온 data를

val checkOutDto = objectMapper.readValue<CheckOutDto>(jsonMessage)

위와 같이 사용하려고 하면 아래와 같은 에러가 발생하게 된다.

(no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

해결 법은 간단하다 다음과 같이 ObjectMapper에 registerModule을 달아 주면된다.

ObjectMapper().registerModule(KotlinModule())

하지만 이는 deprecated가 되었다고 ide에서 경고를 준다. 현재 2023년 3월 기준 최신 방법인 다음과 같이 달아주면 된다.

image

return ObjectMapper().registerKotlinModule()

필자(saechimdaeki) 는 보통 경우 ObjectMapper를 다음과 같이 Bean으로 등록해서 사용함.

@Bean
fun customObjectMapper() : ObjectMapper {
        return ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .registerModule(JavaTimeModule())
            .registerKotlinModule()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant