2012-06-07 100 views
1

我正在嘗試開始與傑克遜一起序列化具有Joda DateTime成員的對象。傑克遜JodaTime DateTime未序列化

我創建了以下串行:

public class DateTimeSerializer extends JsonSerializer<DateTime> { 
    private static DateTimeFormatter formatter = 
    DateTimeFormat.forPattern("dd-MM-yyyy HH:mm:ss, SSS [z]"); 

    @Override 
    public void serialize(DateTime value, JsonGenerator gen, 
     SerializerProvider arg2) throws IOException, 
     JsonProcessingException { 
     gen.writeString(formatter.print(value)); 
    } 
    @Override 
    public Class<DateTime> handledType() { 
     return DateTime.class; 
    } 
} 

,我試圖序列具有DateTime會員我的類,如下所示:

Version version = new Version(1, 0, 0, "SNAPSHOT"); 
    SimpleModule module = new SimpleModule("ZORRO", version); 
    module = module.addSerializer(new DateTimeSerializer()); 
    // and so on... 
    ObjectMapper mapper = new ObjectMapper(); 
    mapper.registerModule(module); 

    mapper.writeValue(new File("C:\\tmp\\window1.json"), window1); 

我收到以下異常(我不明白什麼我做錯了):

java.lang.TypeNotPresentException: Type org.joda.convert.ToString not present 
at com.ibm.oti.reflect.AnnotationHelper.getAnnotation(AnnotationHelper.java:38) 
at com.ibm.oti.reflect.AnnotationHelper.getDeclaredAnnotations(AnnotationHelper.java:50) 
at com.ibm.oti.reflect.Method.getDeclaredAnnotations(Method.java:31) 
at java.lang.reflect.Method.getDeclaredAnnotations(Method.java:687) 
at org.codehaus.jackson.map.introspect.AnnotatedClass._addMixUnders(AnnotatedClass.java:973) 
at org.codehaus.jackson.map.introspect.AnnotatedClass._addMemberMethods(AnnotatedClass.java:637) 
at org.codehaus.jackson.map.introspect.AnnotatedClass.resolveMemberMethods(AnnotatedClass.java:413) 
at org.codehaus.jackson.map.introspect.BasicClassIntrospector.classWithCreators(BasicClassIntrospector.java:185) 
at org.codehaus.jackson.map.introspect.BasicClassIntrospector.collectProperties(BasicClassIntrospector.java:157) 
at org.codehaus.jackson.map.introspect.BasicClassIntrospector.forSerialization(BasicClassIntrospector.java:96) 
at org.codehaus.jackson.map.introspect.BasicClassIntrospector.forSerialization(BasicClassIntrospector.java:16) 
at org.codehaus.jackson.map.SerializationConfig.introspect(SerializationConfig.java:973) 
at org.codehaus.jackson.map.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:251) 
at org.codehaus.jackson.map.ser.StdSerializerProvider._createUntypedSerializer(StdSerializerProvider.java:782) 
at org.codehaus.jackson.map.ser.StdSerializerProvider._createAndCacheUntypedSerializer(StdSerializerProvider.java:758) 
at org.codehaus.jackson.map.ser.StdSerializerProvider.findValueSerializer(StdSerializerProvider.java:380) 
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.resolve(BeanSerializerBase.java:307) 
at org.codehaus.jackson.map.ser.impl.SerializerCache.addAndResolveNonTypedSerializer(SerializerCache.java:159) 
at org.codehaus.jackson.map.ser.StdSerializerProvider._createAndCacheUntypedSerializer(StdSerializerProvider.java:744) 
at org.codehaus.jackson.map.ser.StdSerializerProvider.findValueSerializer(StdSerializerProvider.java:344) 
at org.codehaus.jackson.map.ser.StdSerializerProvider.findTypedValueSerializer(StdSerializerProvider.java:420) 
at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:601) 
at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256) 
at org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2575) 
at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:2048) 
+0

joda-convert在你的類路徑? – Barend

回答

1

你有沒有t他可以在你的類路徑上選擇joda-convert jar?

順便說一句,這是運行時還是編譯時錯誤?