Skip to content

Commit

Permalink
Feat: json 관련 어노테이션 - @JsonInclude
Browse files Browse the repository at this point in the history
  • Loading branch information
bohub12 committed Sep 27, 2023
1 parent 93820ec commit 9fe07fc
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
import com.example.demo.application.dto.MemberJsonIgnorePropertySettersResponse;
import com.example.demo.application.dto.MemberJsonIgnoreResponse;
import com.example.demo.application.dto.MemberJsonIgnoreTypeResponse;
import com.example.demo.application.dto.jsoninclude.JsonIncludeAlwaysResponse;
import com.example.demo.application.dto.jsoninclude.JsonIncludeNonAbsentResponse;
import com.example.demo.application.dto.jsoninclude.JsonIncludeNonDefaultResponse;
import com.example.demo.application.dto.jsoninclude.JsonIncludeNonEmptyResponse;
import com.example.demo.application.dto.jsoninclude.JsonIncludeNonNullResponse;
import com.example.demo.infrastructure.MemberRepository;
import com.example.demo.infrastructure.TeamRepository;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -68,6 +74,31 @@ public List<MemberJsonIgnoreTypeResponse> getDataUsingJsonIgnoreType() {
return memberRepository.findAll().stream().map(MemberJsonIgnoreTypeResponse::new).toList();
}

@GetMapping("/include-always")
public JsonIncludeAlwaysResponse getDataUsingJsonIncludeByAlways() {
return JsonIncludeAlwaysResponse.create();
}

@GetMapping("/include-non-null")
public JsonIncludeNonNullResponse getDataUsingJsonIncludeByNonNull() {
return JsonIncludeNonNullResponse.create();
}

@GetMapping("/include-non-absent")
public JsonIncludeNonAbsentResponse getDataUsingJsonIncludeByNonAbsent() {
return JsonIncludeNonAbsentResponse.create();
}

@GetMapping("/include-non-empty")
public JsonIncludeNonEmptyResponse getDataUsingJsonIncludeByNonEmpty() {
return JsonIncludeNonEmptyResponse.create();
}

@GetMapping("/include-non-default")
public JsonIncludeNonDefaultResponse getDataUsingJsonIncludeByNonDefault() {
return JsonIncludeNonDefaultResponse.create();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.demo.application.dto.jsoninclude;

import com.example.demo.application.MemberRole;
import com.example.demo.domain.Team;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
@JsonInclude(Include.ALWAYS)
public class JsonIncludeAlwaysResponse {
private int defaultInt;
private int normalInt;

private Integer defaultWrapper;
private Integer normalWrapper;

private String nullString;
private String emptyString;
private String normalString;

private Team nullReference;
private Team normalReference;

private List<String> emptyList;
private List<String> normalList;

public static JsonIncludeAlwaysResponse create() {
return new JsonIncludeAlwaysResponse(0, 10,
0, 10,
null, "", "string",
null, new Team("teamA"),
new ArrayList<>(), List.of("str1")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.demo.application.dto.jsoninclude;

import com.example.demo.application.MemberRole;
import com.example.demo.domain.Team;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
@JsonInclude(Include.NON_ABSENT)
public class JsonIncludeNonAbsentResponse {
private int defaultInt;
private int normalInt;

private Integer defaultWrapper;
private Integer normalWrapper;

private String nullString;
private String emptyString;
private String normalString;

private Team nullReference;
private Team normalReference;

private List<String> emptyList;
private List<String> normalList;

public static JsonIncludeNonAbsentResponse create() {
return new JsonIncludeNonAbsentResponse(0, 10,
0, 10,
null, "", "string",
null, new Team("teamA"),
new ArrayList<>(), List.of("str1")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.demo.application.dto.jsoninclude;

import com.example.demo.application.MemberRole;
import com.example.demo.domain.Team;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
@JsonInclude(Include.NON_DEFAULT)
public class JsonIncludeNonDefaultResponse {
private int defaultInt;
private int normalInt;

private Integer defaultWrapper;
private Integer normalWrapper;

private String nullString;
private String emptyString;
private String normalString;

private Team nullReference;
private Team normalReference;

private List<String> emptyList;
private List<String> normalList;

public static JsonIncludeNonDefaultResponse create() {
return new JsonIncludeNonDefaultResponse(0, 10,
0, 10,
null, "", "string",
null, new Team("teamA"),
new ArrayList<>(), List.of("str1")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.demo.application.dto.jsoninclude;

import com.example.demo.application.MemberRole;
import com.example.demo.domain.Team;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
@JsonInclude(Include.NON_EMPTY)
public class JsonIncludeNonEmptyResponse {
private int defaultInt;
private int normalInt;

private Integer defaultWrapper;
private Integer normalWrapper;

private String nullString;
private String emptyString;
private String normalString;

private Team nullReference;
private Team normalReference;

private List<String> emptyList;
private List<String> normalList;

public static JsonIncludeNonEmptyResponse create() {
return new JsonIncludeNonEmptyResponse(0, 10,
0, 10,
null, "", "string",
null, new Team("teamA"),
new ArrayList<>(), List.of("str1")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.demo.application.dto.jsoninclude;

import com.example.demo.application.MemberRole;
import com.example.demo.domain.Team;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
@JsonInclude(Include.NON_NULL)
public class JsonIncludeNonNullResponse {
private int defaultInt;
private int normalInt;

private Integer defaultWrapper;
private Integer normalWrapper;

private String nullString;
private String emptyString;
private String normalString;

private Team nullReference;
private Team normalReference;

private List<String> emptyList;
private List<String> normalList;

public static JsonIncludeNonNullResponse create() {
return new JsonIncludeNonNullResponse(0, 10,
0, 10,
null, "", "string",
null, new Team("teamA"),
new ArrayList<>(), List.of("str1")
);
}
}

0 comments on commit 9fe07fc

Please sign in to comment.