2016-02-12 17 views
9

プロパティアクセス時にRuntimeExceptionsをランダムにスローするようなベンダーからの多数のクラスがあります。私は、クラスを変更することはできませんゲッターが例外をスローするとジャクソンはプロパティを無視する方法

public Object getSomeProperty() { 
    if (!someObscureStateCheck()) { 
     throw new IllegalStateExcepion(); 
    } 
    return calculateTheValueOfProperty(someRandomState); 
} 

、注釈を追加し、スタックのこの部分は、かなり頻繁に変わるように、一つ一つのクラスにミックスインを定義するのは非現実的だことはできません。

ゲッターが例外をスローすると、ジャクソンはプロパティを無視しますか?

+0

try catchを使用することはできますか? –

+1

私はカスタムの 'JsonGenerator'を使ってオブジェクトマッパーを設定することを検討しています。おそらく、[デフォルトの実装]を拡張して変更します(http://fasterxml.github.io/jackson-core/javadoc/2.4/com/fasterxml/ jackson/core/json/JsonGeneratorImpl.html)を使用して、例外を無視/処理します。 – Vulcan

答えて

9

custom serializationを実行するには、register a moduleBeanSerializerModifierと入力し、必要な変更を指定します。あなたのケースでは、BeanPropertyWriter.serializeAsFieldは個々のフィールドをシリアル化するメソッドなので、フィールド直列化の例外を無視する独自のBeanPropertyWriterを作成し、changePropertiesを使用する​​のモジュールを登録して、デフォルトのBeanPropertyWriterインスタンスを独自の実装に置き換えます。以下の実施例は:

import com.fasterxml.jackson.core.JsonGenerator; 
import com.fasterxml.jackson.databind.*; 
import com.fasterxml.jackson.databind.module.SimpleModule; 
import com.fasterxml.jackson.databind.ser.*; 

import java.io.IOException; 
import java.util.List; 
import java.util.stream.Collectors; 

public class JacksonIgnorePropertySerializationExceptions { 

    public static void main(String[] args) throws IOException { 
     ObjectMapper mapper = new ObjectMapper(); 
     mapper.registerModule(new SimpleModule().setSerializerModifier(new BeanSerializerModifier() { 
      @Override 
      public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { 
       return beanProperties.stream().map(bpw -> new BeanPropertyWriter(bpw) { 
        @Override 
        public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception { 
         try { 
          super.serializeAsField(bean, gen, prov); 
         } catch (Exception e) { 
          System.out.println(String.format("ignoring %s for field '%s' of %s instance", e.getClass().getName(), this.getName(), bean.getClass().getName())); 
         } 
        } 
       }).collect(Collectors.toList()); 
      } 
     })); 

     mapper.writeValue(System.out, new VendorClass()); 
    } 

    public static class VendorClass { 
     public String getNormalProperty() { 
      return "this is a normal getter"; 
     } 

     public Object getProblematicProperty() { 
      throw new IllegalStateException("this getter throws an exception"); 
     } 

     public String getAnotherNormalProperty() { 
      return "this is a another normal getter"; 
     } 
    } 
} 

上記のコードはジャクソン2.7.1およびJava 1.8を使用して、次のように出力:

IllegalStateExceptionをスロー getProblematicPropertyは、シリアライズから省略されることを示す
ignoring java.lang.reflect.InvocationTargetException for field 'problematicProperty' of JacksonIgnorePropertySerializationExceptions$VendorClass instance 
{"normalProperty":"this is a normal getter","anotherNormalProperty":"this is a another normal getter"} 

値。

関連する問題