@ConstructorProperties 주석은 java.bean에서 가져온 것입니다. s 패키지, 주석이 달린 생성자를 통해 JSON을 자바 객체로 역직렬화하는 데 사용 . 이 주석은 Jackson 2.7 버전부터 지원합니다. 앞으로. 이 주석이 작동하는 방식은 생성자의 각 매개변수에 주석을 달지 않고 매우 간단합니다. 배열에 각 생성자 매개변수의 속성 이름을 제공할 수 있습니다.
구문
@Documented @Target(value=CONSTRUCTOR) @Retention(value=RUNTIME) public @interface ConstructorProperties
예시
import com.fasterxml.jackson.databind.ObjectMapper;
import java.beans.ConstructorProperties;
public class ConstructorPropertiesAnnotationTest {
public static void main(String args[]) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Employee emp = new Employee(115, "Raja");
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
System.out.println(jsonString);
}
}
// Employee class
class Employee {
private final int id;
private final String name;
@ConstructorProperties({"id", "name"})
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getEmpId() {
return id;
}
public String getEmpName() {
return name;
}
} 출력
{
"empName" : "Raja",
"empId" : 115
}