Skip to content

[Jackson] DTO 특정 필드 응답에서 제외시키기 #11

@dkdud9261

Description

@dkdud9261

MongoDB를 사용 중이라면,

@Document
data class MyCollection(
  @id
  val id: String? = null,
  val name: String,
  val content: String
)

요런 식으로 Document 클래스를 정의할테고,
API 응답 바디로 대게는 다른 DTO를 만들어 사용할테지만,
간단하게 하느라 요 Document 클래스를 그대로 사용해야 한다면?

id 값은 보통 클라이언트에 노출하지 않기 때문에 요 필드만 제외하고 응답을 반환하고 싶다.
이럴 때는 새로 DTO를 정의할 필요 없이, 제외하고 싶은 필드에 @JsonIgnore만 추가하면 직렬화 시에 제외된다.

예제)

@Document
data class MyCollection(
  @id
  @JsonIgnore
  val id: String? = null,
  val name: String,
  val content: String
)

응답 예시

{
  "name" : "testName",
  "content" : "testContent"
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions