# 필드값이 Null일 때 무시하기(Jackson)

# Assignment

@JsonPropertyOrder({
        "something"
})
public class SomeClass {

    @Getter @Setter
    private String something;
}
1
2
3
4
5
6
7
8

만일 Field 값이 Null이면 어떻게 하면 무시할수 있을까?

# Solution

  1. 직접 ObjectMapper에 설정 (opens new window)할수 있다.
mapper.setSerializationInclusion(Include.NON_NULL);
1
  1. @JsonInclude 어너테이션 사용 (opens new window)
@JsonInclude(Incldue.NON_NULL)
public class SomeClass {

    @Getter @Setter
    private String something;
}
1
2
3
4
5
6

만일 하나의 필드만 설정하고 싶으면 아래와 같이 하면 된다.

public class SomeClass {

    @Getter @Setter
    @JsonInclude(Incldue.NON_NULL)
    private String something;
}
1
2
3
4
5
6

Let`s converts caffeine into code ☕️