Java ObjectMapper로 readValue할 때 DTO에 원하는 값만 작성하기

@ZungTa · 2024-01-22 월요일 · 1 min read

JsonString을 ObjectMapper를 통해서 Java의 Class로 변환할 때 JsonString에 존재하는 모든 값을 Class의 Property로 넣어주지 않으면 오류가 발생한다.

원하는 값만 Class로 작성해주고 싶다면 @JsonIgnoreProperties를 이용하면 된다.

@JsonIgnoreProperties(ignoreUnknown = true)를 사용하면 모르는 값은 무시한다.

@JsonIgnoreProperties(ignoreUnknown = true)
public class UserDto {
  String userName;
  Integer userAge;
}

References

@ZungTa
I'm a backend developer
© ZungTa Devlog, Built with Gatsby