Assume, I have the following class:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ObjectOfMonitoring {
private BigInteger id;
private Map<String, Object> properties = new HashMap<>();
@JsonAnySetter
public void add(String key, Object value) {
properties.put(key, value);
}
@JsonAnyGetter
public Map<String, Object> getProperties() {
return properties;
}
I test it in the following code:
ObjectOfMonitoring objectOfMonitoring = new ObjectOfMonitoring();
objectOfMonitoring.setId(BigInteger.ONE);
objectOfMonitoring.add("key1", "value1");
String jsonInString = mapper.writeValueAsString(objectOfMonitoring);
System.out.println(jsonInString);
I want to get result:
{"id":1,"key2":"value2","key1":"value1"}
But actual result is:
{"id":1,"properties":{"key2":"value2","key1":"value1"}}
What do I do incorrectly? And how to get the expected result?