2016-03-31 13 views
-3
package com.dh.inheritance; 

import com.fasterxml.jackson.annotation.JsonAutoDetect; 
import com.fasterxml.jackson.annotation.JsonTypeInfo; 
import com.fasterxml.jackson.annotation.PropertyAccessor; 
import com.fasterxml.jackson.core.JsonProcessingException; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.SerializationFeature; 


public class Client6{ 

    public static void main(String[] args){ 

     ObjectMapper objectMapper = new ObjectMapper(); 
     objectMapper.enableDefaultTyping(); 
     objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);   
     objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE); 
     objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE); 
     objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); 
     objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);   
     objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true); 
     objectMapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, true); 

     try { 

      String test = null; 
      test.length(); 

     } catch (Exception e) { 

      try { 
       String jsonString = objectMapper.writeValueAsString(e); 
       System.out.println(jsonString); 
      } catch (JsonProcessingException e1) { 
       e1.printStackTrace(); 
      } 
     } 
    } 

} 

上記のコードを実行すると、私は例外以下になります。私にこの問題を解決する方法を教えてください。ジャクソンを使用してJava例外をシリアライズ

com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: java.lang.NullPointerException["cause"]) 
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:230) 
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._handleSelfReference(BeanPropertyWriter.java:879) 
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:666) 
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678) 
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:569) 
    at com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer.serialize(TypeWrappedSerializer.java:32) 
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130) 
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3613) 
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2980) 
    at com.dh.inheritance.Client6.main(Client6.java:33) 
+1

http://stackoverflow.com/questions/10446891/jackson-self-reference-leading-to -cycle – Stefan

+0

Stefen返信ありがとうございます。あなたが言ったように私を試してみましょう。 – Partha

答えて

1

json文字列として書き込むコードの下で動作しています。

objectMapper.registerModule(new MyModule());

package com.dh.inheritance; 

import com.fasterxml.jackson.annotation.JsonIdentityInfo; 
import com.fasterxml.jackson.annotation.ObjectIdGenerators; 


public abstract class MixIn { 

    @JsonIdentityInfo(generator=ObjectIdGenerators.StringIdGenerator.class, property="$id") 
    private Throwable cause; 

} 

package com.dh.inheritance; 

import com.fasterxml.jackson.core.Version; 
import com.fasterxml.jackson.databind.module.SimpleModule; 

public class MyModule extends SimpleModule{ 

    public MyModule() { 
     super("test", new Version(2, 7, 2, null)); 
    } 

    @Override 
    public void setupModule(SetupContext context) { 
     context.setMixInAnnotations(Throwable.class, MixIn.class);  
    } 

} 

しかし、jsonからObjectへの読み込みに失敗しました。 出力:

{ "java.lang.NullPointerExceptionが":{ "detailMessage":ヌル、 "原因":{ "java.lang.NullPointerExceptionが":{ "$のID": "912da540-bf85-4e5d "StackTrace":null}} com.fasterxml.jackson "、" stackTrace ":null}}、" stackTrace ":null}}、" stackTrace ":null}}、" detailMessage ":null、"原因 ":" 912da540-bf85-4e5d-869c-acfaebe85bdf " (原因: "cause"、 "stackTrace"、 "detailMessage"])で指定されていない フィールドが認識されませんでした。 "" "detailMessage":null、 "cause":{"java.lang.NullPointerException":{"$ id": "912da540-bf85-4e5d-869c-acfaebe85b: {" java.lang.NullPointerException " df "、" detailMessage ":null、" cause ":" 912da540-bf85-4e5d-869c-acfaebe85bdf "、" stackTrace ":null}}、" stackTrace ":null}}; (参照番号チェーン: java.lang.NullPointerException ["$ id"])) com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62) at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:855) でcom.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1083) com.fasterxmlで.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1389) でcom.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:135) でcom.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133) com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer._deserialize(AsWrapperTypeDeserializer.java:115) でで で com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer.deserializeTypedFromObject com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1017)で(AsWrapperTypeDeserializer.java:49) コム.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:488) at com.fasterxml.jackson.databind.deser.std.ThrowableDeseri alizer.deserializeFromObject(ThrowableDeserializer.java:104) でcom.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133) com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializerで。 で_deserialize(AsWrapperTypeDeserializer.java:115) com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithTypeでcom.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer.deserializeTypedFromObject(AsWrapperTypeDeserializer.java:49) ( BeanDeserializerBase.java:1017) at com.fasterxml.jackson.databind。 com.fasterxml.jackson.databind.ObjectMapper.readValueでcom.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789) ( でdeser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:42) ObjectMapper.java:2779)com.dh.inheritance.Client6.main(Client6.java:45)で

関連する問題