@ConstructorProperties एनोटेशन java.beans पैकेज से है, जिसका उपयोग एनोटेटेड कंस्ट्रक्टर के माध्यम से JSON को Java ऑब्जेक्ट में डिसीरियलाइज़ करने के लिए किया जाता है। यह एनोटेशन 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 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 }