1

我正在尝试开始使用杰克逊来序列化具有 JodaDateTime成员的对象。

我创建了以下序列化程序:

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)
4

2 回答 2

1

你的类路径上有可选的joda-convert jar 吗?

顺便说一句,这是运行时错误还是编译时错误?

于 2012-06-07T12:43:39.140 回答
0

不是 Joda DateTime 是不可序列化的,而是 DateTimeFormatter。

查看那些 2 的类签名:

public final class DateTime
        extends BaseDateTime
        implements ReadableDateTime, Serializable {

public class DateTimeFormat {

现在很明显,您不应该尝试序列化格式化程序。但是 DateTime 本身非常好,因为它实现了java.io.Serializable.

于 2018-11-26T19:11:10.217 回答