Here is my class
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.*;
public class Util {
public static void main(String[] args) throws JsonProcessingException {
String json = "{ \"argA\" : 5, \"unneededkey\" : 6}";
ObjectMapper mapper = new ObjectMapper();
MyObject object3 = mapper.readValue(json, MyObject.class);
System.out.println(object3);
}
@ToString
@RequiredArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public static class MyObject {
public MyObject(int argA) {
this.argA = argA;
}
public MyObject(int argA, boolean argB) {
this.argA = argA;
this.argB = argB;
}
public MyObject(int argA, int argC, int argD) {
this.argA = argA;
this.argC = argC;
this.argD = argD;
}
public MyObject(int argA, String argE) {
this.argA = argA;
this.argE = argE;
}
public int argA = 1;
public boolean argB;
public int argC = 4;
public int argD = 5;
public String argE;
}
}
@JsonIgnoreProperties(ignoreUnknown = true) works but this ignores all unneeded keys. What if I only want to ignore certain unneeded keys and if I am given a key thats not part of my object constructor or in the unneeded set, it will throw an exception.