nobel6018 / utterances

0 stars 0 forks source link

devs/86 #7

Open utterances-bot opened 2 months ago

utterances-bot commented 2 months ago

Kotlin SpringBoot에서 Jackson Library

Kotlin SpringBoot에서 Jackson Library 동작법을 기록으로 남겨둡니다.

https://v3.leedo.me/devs/86

nobel6018 commented 2 months ago

val이 없으면 해당 필드는 직렬화될 때 표시되지 않습니다 왜냐하면, jackson 라이브러리는 serialize할 때 내부적으로 getter를 사용하는데, val이 없으니 getter가 없습니다

예시:

class Person(
    val name: String,
    age: Int
)

json 결과:

{
  "name": "user"
}

jackson은 deserialize할 때 primary constructor를 사용하는데 이 때 문제가 생깁니다 ⛑️ Person 클래스는 name, age 필드를 둘 다 요구하는데 json에서는 age가 없기 때문에 MissingKotlinParameterException: due to missing (therefore NULL) value for creator parameter … which is a non-nullable type 에러가 발생합니다

정리하면, val 키워드가 없는 경우, 직렬화(serialize) 때는 괜찮은데, 역직렬화(deserialize)할 때 에러가 발생하게 됩니다