JAVA/Spring Boot
[JAVA] Response - Null 제외(@JsonInclude)
남기루
2021. 12. 9. 13:42
반응형
@JsonInclude 어노데이션을 사용해 Response에 포함될 필드를 설정할 수 있다.
Include.ALWAYES.. 등과 함께 사용한다.
public static enum Include {
ALWAYS,
NON_NULL,
NON_ABSENT,
NON_EMPTY,
NON_DEFAULT,
CUSTOM,
USE_DEFAULTS;
private Include() {
}
}
예시
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@JsonInclude(Include.NON_NULL)
public class CategoryResponseModel {
private final String type;
private final String name;
private final String icon;
public CategoryResponseModel(CategoryDTO categoryDTO) {
this.type = categoryDTO.getType();
this.name = categoryDTO.getName();
this.icon = categoryDTO.getIcon();
}
public String getType() {
return type;
}
public String getName() {
return name;
}
public String getIcon() {
return icon;
}
}
반응형