Jackson Annotations 简明教程

Jackson Annotations - @JsonAnySetter

@JsonAnySetter 允许设置程序使用Map,然后用于以与其他属性类似的方式反序列化JSON的附加属性。

Example @JsonAnySetter

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonTester {
   public static void main(String args[]){
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = "{\"RollNo\" : \"1\",\"Name\" : \"Mark\"}";
      try {
         Student student = mapper.readerFor(Student.class).readValue(jsonString);
         System.out.println(student.getProperties().get("Name"));
         System.out.println(student.getProperties().get("RollNo"));
      }
      catch (IOException e) {
         e.printStackTrace();
      }
   }
}
class Student {
   private Map<String, String> properties;
   public Student(){
      properties = new HashMap<>();
   }
   public Map<String, String> getProperties(){
      return properties;
   }
   @JsonAnySetter
   public void add(String property, String value){
      properties.put(property, value);
   }
}

Output

Mark
1