例子
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class MyTest21 {
public static class Phone {
private String id;
private Map<String,Object> other = new HashMap<>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@JsonAnyGetter
public Map<String, Object> getOther() {
return other;
}
@JsonAnySetter
public void setOther(String key, Object value) {
this.other.put(key,value);
}
@Override
public String toString() {
return "Phone{" +
"id='" + id + '\'' +
", other=" + other +
'}';
}
}
@Test
public void test() throws Exception{
Phone p=new Phone();
p.id="id";
p.other.put("name","name");
var mapper= new ObjectMapper();
String s = mapper.writeValueAsString(p);
System.out.println(s);
Phone phone = mapper.readValue(s, Phone.class);
System.out.println(phone.toString());
}
}