English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
@ConstructorPropertiesAnnotation is fromjava.beanMini package for deserializing JSON to Java objects Annotation Construction. This annotation comes fromJackson 2.7VersionStarting support. The way this annotation works is very simple, instead of annotating each parameter in the constructor, we can provide the property name of each constructor parameter for the array.
@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; {} {}
Output Result
{ "empName": "Raja", "empId": 115 {}