Reputation:
If I try to deserialize a JSON string that is missing a field from the object class, that field is set to null when the object is created. I need Jackson to throw an exception when this happens so I don't have to implement an assertNotNullFields()
method in every object; i.e the JSON string input must have defined all the fields in the class or a JsonProcessingException
is thrown.
Object mapper configuration:
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
JSON object:
public class LogInUser {
public String identifier, password;
public LogInUser() {}
}
Is this possible given my configuration?
Upvotes: 1
Views: 936
Reputation: 60
You could try using JsonCreator
within your LogInUser
class as mentioned here. When introduced into the code you posed in your quesion, it would look something like this:
public class LogInUser {
@JsonCreator
public LogInUser(@JsonProperty(value = "identifier", required = true) String identifier, @JsonProperty(value = "password", required = true) String password) {
this.identifier = identifier;
this.password = password;
}
public String identifier, password;
public LogInUser() {}
}
When one or more of the values are null, it should throw an exception.
Upvotes: 1