Unrecognized field "address" (class me.kotlin.sample.sample.SimpleData), not marked as ignorable (2 known properties: "first_name", "last_name"]) at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: me.kotlin.sample.sample.SimpleData["ad..

2021. 5. 12. 07:00·아카이브/TroubleShooting
반응형

ObjectMapper를 사용하던 중 에러 발생!

나를 닮았다고 한다...

증상

에러 메시지

Unrecognized field "address" (class me.kotlin.sample.sample.SimpleData), not marked as ignorable (2 known properties: "first_name", "last_name"]) at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: me.kotlin.sample.sample.SimpleData["address"])

 

OtherData 데이터 객체를 SimpleData 데이터 객체로 바꾸는 중 에러 발생 

class OtherData(
    val lastName: String? = null,
    val firstName: String? = null,
    val address: String? = null,
    val age: Int? = null,
)
class SimpleData(
    val lastName: String? = null,
    val firstName: String? = null,
)

 

사용하는 ObjectMapper Util 클래스

class MapperUtils {

    companion object {
        private val objectMapper: ObjectMapper = ObjectMapper()
        
        fun getMapper(): ObjectMapper = this.objectMapper

    }
}

Test Code

@Test
fun `Mapper Convert Test`() {
    // Given
    val otherData = OtherData(
        lastName = "lim",
        firstName = "yongtae",
        address = "강남",
        age = 33
    )


    // When
    val simpleData = MapperUtils.getMapper().convertValue(otherData, SimpleData::class.java)

    // Then
    assertThat(simpleData.lastName).isEqualTo("lim")
}

 

 

원인

Convert하여 데이터를 적용하려는 객체에 address, age 필드가 없어서 에러 발생

 

해결

ObjectMapper에 설정값 추가

class MapperUtils {

    companion object {
        private val objectMapper: ObjectMapper = ObjectMapper()
        
        fun getMapper(): ObjectMapper = getObjectMapper(this.objectMapper)


        private fun getObjectMapper(objectMapper: ObjectMapper) =
            objectMapper
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

    }
}
반응형

'아카이브 > TroubleShooting' 카테고리의 다른 글

Kotlin: Cannot inline bytecode built with JVM target 11 into bytecode that is being built with JVM target 1.8. Please specify proper '-jvm-target' option  (0) 2021.07.05
MockMvc 주입안되는 문제  (0) 2021.04.01
nested exception is java.lang.IllegalArgumentException: No sources given  (0) 2020.12.19
[JPA] java.lang.NoSuchMethodException: org.springframework.data.domain.Pageable.<init>  (0) 2020.12.11
[prometheus] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver  (0) 2020.12.08
'아카이브/TroubleShooting' 카테고리의 다른 글
  • Kotlin: Cannot inline bytecode built with JVM target 11 into bytecode that is being built with JVM target 1.8. Please specify proper '-jvm-target' option
  • MockMvc 주입안되는 문제
  • nested exception is java.lang.IllegalArgumentException: No sources given
  • [JPA] java.lang.NoSuchMethodException: org.springframework.data.domain.Pageable.<init>
에디개발자
에디개발자
------ 한발자국씩 성장하자 ------ Github: https://github.com/yongtaelim LinkedIn: https://www.linkedin.com/in/%EC%9A%A9%ED%83%9C-%EC%9E%84-622b69218/
    250x250
  • 에디개발자
    에디블로그
    에디개발자
    • 분류 전체보기 (278) N
      • AI (67) N
        • Claude (27)
        • ChatGPT (12)
        • Cursor (13)
        • Gemini (10) N
        • 트랜드 (4)
        • 오늘의 AI 업데이트 (1)
      • 개발 트렌드 (29) N
        • 데일리 픽 (25) N
        • 툴 리뷰 (1)
      • 개발자 도구 (7) N
        • 생산성 툴 (3) N
        • 노트 & 지식관리 (0)
        • 협업 & 이슈 (0)
        • 터미널 & 환경 (0)
        • API & DB (3)
        • 에디터 & IDE (1) N
      • 실험실 (1) N
        • AI 자동화 (0)
        • 자동매매 (1) N
        • 블로그 자동화 (0)
      • 아카이브 (168)
        • MindControl (12)
        • TroubleShooting (16)
        • kotlin ( 아카이브 ) (16)
        • java ( 아카이브 ) (15)
        • spring (6)
        • test (4)
        • message-queue (10)
        • object (22)
        • spring-data (28)
        • spring-batch (7)
        • devops (5)
        • java (5)
        • kotlin (3)
        • database (2)
        • Study (3)
        • Develop (1)
        • Develop Tool (1)
        • Daily Develop (7)
        • Book (5)
  • 인기 글

  • 태그

    코틀린
    클로드
    AI 활용
    queryDSL
    OpenAI
    스터디
    Ai
    AI 업데이트
    ai 코딩
    개발자
    에디
    JPA
    프로그래밍
    Gemini
    ChatGPT
    anthropic
    cursor
    ai 정보
    개발자 도구
    claude
  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
에디개발자
Unrecognized field "address" (class me.kotlin.sample.sample.SimpleData), not marked as ignorable (2 known properties: "first_name", "last_name"]) at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: me.kotlin.sample.sample.SimpleData["ad..
상단으로

티스토리툴바