어제 에러해결 이후로 @JsonCreator에 대해 조사해보았다.
@JsonCreator와 @JsonProperty는 Jackson 라이브러리에서 객체의 역직렬화와 직렬화 과정을 제어하기 위해 사용하는 어노테이션이다.
JSON 데이터를 Java 객체로 변환할 때 어떤 생성자를 사용할지를 지정하는 어노테이션
불변 객체의 생성자 기반 역직렬화가 가능해진다.
public class Person {
private String name;
private int age;
// 생성자에 @JsonCreator와 @JsonProperty를 사용하여 JSON 매핑
@JsonCreator
public Person(@JsonProperty("name") String name, @JsonProperty("age") int age) {
this.name = name;
this.age = age;
}
직렬화와 역직렬화 시 JSON 필드명을 매핑하는 데 사용되는 어노테이션 보통 JsonCreator와 함께 사용한다.
Jackson에서 JSON의 필드 이름과 객체의 필드(혹은 생성자의 매개변수) 이름을 매핑할 때 사용된다. 이 어노테이션을 통해 JSON의 속성 이름을 명시적으로 지정할 수 있다.
public class Person {
private String name;
private int age;
@JsonProperty("full_name")
public String getName() {
return name;
}
@JsonProperty("full_name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("years_old")
public int getAge() {
return age;
}
@JsonProperty("years_old")
public void setAge(int age) {
this.age = age;
}
}