
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
When can we call @JsonAnyGetter and @JsonAnySetter annotations in Java?n
The @JsonAnyGetter annotation enables to use a Map as a container for properties that we want to serialize to JSON and @JsonAnySetter annotation instructs Jackson to call the same setter method for all unrecognized fields in the JSON object, which means that all fields that are not already mapped to a property or setter method in the Java object.
Syntax
public @interface JsonAnyGetter public @interface JsonAnyGetter
Example
import java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.*; public class JsonAnyGetterAndJsonAnySetterTest { public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException { Employee emp1 = new Employee(); emp1.setFirstName("Adithya"); emp1.setLastName("Sai"); emp1.setEmpId(125); emp1.getAdditionalInformation().put("technology1", "Machine Learning"); emp1.getAdditionalInformation().put("technology2", "Robotics"); ObjectMapper mapper = new ObjectMapper(); String jsonStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp1); System.out.println(jsonStr); System.out.println("Deserializing JSON to Object:"); Employee emp2 = mapper.readValue(jsonStr, Employee.class); System.out.println("id : " + emp2.getEmpId()); System.out.println("firstName : " + emp2.getFirstName()); System.out.println("lastName : " + emp2.getLastName()); System.out.println("Additional information : " + emp2.getAdditionalInformation()); } } // Employee class @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({"firstName", "lastName", "technologies", "empId" }) class Employee { @JsonProperty("EMPLOYEE_ID") private int empId; @JsonProperty("EMPLOYEE_FIRST_NAME") private String firstName; @JsonProperty("EMPLOYEE_LAST_NAME") private String lastName; private Map<String, String> additionalInformation = new HashMap<>(); public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @JsonAnyGetter public Map<String, String> getAdditionalInformation() { return additionalInformation; } public void setAdditionalInformation(Map<String, String> additionalInformation) { this.additionalInformation = additionalInformation; } @JsonAnySetter public void setAdditionalProperty(final String name, final String value) { this.additionalInformation.put(name, value); } }
Output
{ "EMPLOYEE_FIRST_NAME" : "Adithya", "EMPLOYEE_LAST_NAME" : "Sai", "EMPLOYEE_ID" : 125, "technology1" : "Machine Learning", "technology2" : "Robotics" } Deserializing JSON to Object: id : 125 firstName : Adithya lastName : Sai Additional information : {technology1=Machine Learning, technology2=Robotics}
- Related Articles
- When can we call wait() and wait(long) methods of a Thread in Java?
- When to use @JsonManagedReference and @JsonBackReference annotations using Jackson in Java?
- Can we call methods using this keyword in java?
- How can we call garbage collection (GC) explicitly in Java?
- Can we call Superclass’s static method from subclass in Java?
- How can we call the invokeLater() method in Java?\n
- Can we call a constructor directly from a method in java?
- Can we call run() method directly instead of start() in Java
- When can we use a JSONStringer in Java?
- When can we use Synchronized blocks in Java?
- Can we call the wait() method without acquiring the lock in Java?
- What are annotations in Java?
- Can we call Fission as fragmentation?
- Why use Java annotations?
- Can we call methods of the superclass from a static method in java?

Advertisements