LoginSignup
27

More than 5 years have passed since last update.

Jackson JSON processorで存在しない変数を無視してマッピングする2つの方法

Last updated at Posted at 2014-05-20

JacksonでJSON -> OBjectのマッピングをするとき、JSONには存在して、マッピング対象のクラスには存在しないフィールドがあると、

org.codehaus.jackson.map.exc.UnrecognizedPropertyException

が発生します。

回避方法1:Annotation

マッピング対象のクラスに@JsonIgnorePropertiesをつける

@JsonIgnoreProperties(ignoreUnknown = true)
public class Something implements Serializable {
    ...(skip)
}

回避方法2:Configure

ObjectMapperにパラメータを設定する

// Jackson 1.9 and before
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// Jackson 2.0
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

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
27