1

I know that jackson can ignore missing fields from a JSON during java serialization and also it can ignore unknown fields too.

I'd like to know if there's a way to make jackson throw an exception when it find an unknown field.

I think in jackson internals, probably it uses SAX and just care about "events" related to known attributes, which is also better for performance reasons.

However, detecting a malformed JSON (in a sense that it contains unknown attributes, not in a sense of JSON format itself of course) is helpful specially when users are creating big JSONs by hand.

Leo
  • 751
  • 4
  • 29
  • 1
    org.codehaus.jackson.map.exc.UnrecognizedPropertyException do we get this ? – VedantK Jan 26 '18 at 12:48
  • 1
    do you want exception to be thrown on unknown field right? – VedantK Jan 26 '18 at 12:50
  • As @VedX stated, by default UnrecognizedPropertyException is thrown when you are trying to unmarshal JSON. – heroin Jan 26 '18 at 12:53
  • you guys are right. my question is not good. this happens because I am using Spring - https://stackoverflow.com/questions/14343477/how-do-you-globally-set-jackson-to-ignore-unknown-properties-within-spring – Leo Jan 26 '18 at 16:19
  • I will close the question since it's dup – Leo Jan 26 '18 at 16:19

1 Answers1

3

You can configure your object mapper.

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);

Reference here

VedantK
  • 9,728
  • 7
  • 66
  • 71