Jackson: Json annotations and dyna beans

The json string at times have a lot of properties. It seems a waste creating a POJO with all those properties. Wouldnt it be great if there was a catch’all that could read all properties in a map? Jackson provides annotations to do just that. In the example below we have set two properties in the bean and the other properties are read into a map. These example also introduces some common annotations using in Jackson. Lets look at them briefly:

  1. @JsonProperty-This annotation is used to mark a method as a getter or setter for a property. In other words, it associates a json field with a java property. If a name is specified (@JsonProperty(“age”)) then the java property that is annotated with this annotation is mapped to the ‘age’ field of the json, If no name is specified the java property name is used.
  2. @JsonCreator-This annotation is used the define constructors that are used to create java objects from json string. It is used during data binding and specifies properties that will be used to create java objects during deserialization.
  3. @JsonAnyGetter and @JsonAnySetter - This annotations are used to mark methods that set or read fields that are not handled by any other java property. These act like catch-all and handle all fields that are not handled by any other java property. The fields are stored in a map as key value pairs.

Data Binding Example


package com.studytrails.json.jackson;

public class AlbumsDynamic {

    private String title;
    private DatasetDynamic[] dataset;

    public void setTitle(String title) {
        this.title = title;

    public void setDataset(DatasetDynamic[] dataset) {
        this.dataset = dataset;

    public String getTitle() {
        return title;

    public DatasetDynamic[] getDataset() {
        return dataset;

Dataset class

package com.studytrails.json.jackson;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class DatasetDynamic {
    private String album_id;
    private String album_title;
    private Map<String , Object> otherProperties = new HashMap<String , Object>();

    public DatasetDynamic(@JsonProperty("album_id") String album_id, @JsonProperty("album_title") String album_title) {
        this.album_id = album_id;
        this.album_title = album_title;

    public String getAlbum_id() {
        return album_id;

    public void setAlbum_id(String album_id) {
        this.album_id = album_id;

    public String getAlbum_title() {
        return album_title;

    public void setAlbum_title(String album_title) {
        this.album_title = album_title;

    public Object get(String name) {
        return otherProperties.get(name);

    public Map<String , Object> any() {
        return otherProperties;

    public void set(String name, Object value) {
        otherProperties.put(name, value);

(Note: This article’s original links is here )

 Jackson: List serialization Jackson: Json data binding filters 


©2015-2022 Alimy All rights reserved.