
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When to use @ConstructorProperties annotation with Jackson in Java?
The @ConstructorProperties annotation is from java.beans package, used to deserialize JSON to java object via the annotated constructor. This annotation supports from Jackson 2.7 version onwards. The way this annotation works very simple, rather than annotating each parameter in the constructor, we can provide an array with the properties names for each of the constructor parameters.
Syntax
@Documented @Target(value=CONSTRUCTOR) @Retention(value=RUNTIME) public @interface ConstructorProperties
Example
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
{ "empName" : "Raja", "empId" : 115 }
- Related Questions & Answers
- When to use @JsonValue annotation using Jackson in Java?
- When to use @JsonAutoDetect annotation in Java?
- What is the use of @JacksonInject annotation using Jackson in Java?
- What is the use of @JsonRawValue annotation using Jackson API in Java?
- Importance of the Jackson @JsonInclude annotation in Java?
- Importance of @JsonRootName annotation using Jackson in Java?
- Importance of @JsonView annotation using Jackson in Java?
- Importance of @JsonIdentityInfo annotation using Jackson in Java?
- Importance of @JsonUnwrapped annotation using Jackson in Java?
- When to use @JsonManagedReference and @JsonBackReference annotations using Jackson in Java?
- What to use @SerializedName annotation using Gson in Java?
- How to use @Until annotation using the Gson library in Java?
- When to use an abstract class and when to use an interface in Java?
- When to use static methods in Java?
- When to use vararg methods in Java?
Advertisements