I have the following class:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonNaming; import lombok.Data; @Data @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) @JsonIgnoreProperties(ignoreUnknown = false) public class Header { private String myName; private String client; private String createdOn; private String updatedOn; }
and my mapper looks as follows:
public static <T> T fromJson(String json, Class<T> clazz) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper() .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); return mapper.readValue(json, clazz); }
Today, if someone messes up their json object for example messing up the capitalization on client
:
"my_name": "John Doe", "cLient": "Foo Bar", "created_on": "12-08-2021", "updated_on": "06-09-2022"
it will deserialize client
to null. I’d like it to error because the casing is wrong. (And for any key that’s passed in with bad casing).
Is there a way to do this with config? If no, how?
Advertisement
Answer
You can accomplish this in your yaml.
spring: jackson: deserialization: FAIL_ON_UNKNOWN_PROPERTIES: true
Not sure why the object mapper wasn’t hooking up.