@JsonProperty
를 사용하여 json key값을 명시한다.{
"code": "TEST1",
"description": "테스트 코드입니다."
}
@Getter
@ToString
public class CodeDto {
private String code;
private String description;
@JsonCreator
public JsonCreatorDTO(@JsonProperty("code") String code, @JsonProperty("description") String description) {
this.code = code;
this.description = description;
}
}
getter
에 @JsonValue
해당 멤버 필드가 이름을 통해 직렬화 시킨다.Enum
사용 시, 특정 값만 리턴하고 싶다면, 변수에 @JsonValue
어노테이션을 추가해준다.public enum EnumWithValue {
ENUM1(1, "TYPE1"),
ENUM2(2, "TYPE2");
private Integer intValue;
private String strValue;
private EnumWithValue(Integer intValue, String strValue) {
this.intValue = intValue;
this.strValue = strValue;
}
@JsonValue
public Integer getIntValue() {
return intValue;
}
public String getStrValue() {
return strValue;
}
}
@AllArgsConstructor
@Getter
public enum Foo {
A("에이", "a"),
B("비", "b"),
C("씨", "c");
@JsonValue
private String korean;
private String small;
}
LocalDate
/ LocalDateTime
을 JSON으로 직렬화 할때 포맷을 관리한다.Enum
사용 시, 객체 전체 리턴을 원하는 경우, @JsonFormat
어노테이션을 추가해준다.Shape.OBJECT
로 설정해준다.@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime startDay;
@JsonFormat(shape = Shape.OBJECT)
@AllArgsConstructor
@Getter
public enum Foo {
A("에이", "a"),
B("비", "b"),
C("씨", "c");
private String korean;
private String small;
}
@JsonIgnore
private Map<String, Object> tmpData;
출처
Jackson Deserialization Annotation
Spring Boot - 직렬화!
Jackson의 모든 것 - 역 직렬화 편
Jackson의 모든 것 - 직렬화 편
@JsonIgnore 이란?
[JAVA] Spring Boot json Enum json object or jsonvalue
[Spring Boot] json response할 때 Enum 객체 전체 보여주기
[Spring boot] 날짜 타입 JSON 변환