LoginSignup
1
0

More than 1 year has passed since last update.

ObjectMapperについて

Last updated at Posted at 2022-02-23

概要

jackson-databindに含まれるObjectMapperについて疑問に思うところがあったため、調査した記録を残す。

1. [readValue] recordにreadValueすることはできるか?

できる。
recordクラスのフィールドは自ずとfinalになるため、もしかしたらデシリアライズできないのではないか?と思っていたが杞憂であった。

2. [readValue] jsonに想定しないpropertyが入っている場合にはどうなるか?

UnrecognizedPropertyExceptionをthrowする。
もし想定外のpropertyが来たとしても無視したいならば、下記のようにObjectMapperのFAIL_ON_UNKNOWN_PROPERTIESをdisableしてやれば良い。

        ObjectMapper disableFailOnUnknownPropertiesObjectMapper = new ObjectMapper()
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

検証コード

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0