2017-11-04 61 views
2

我试图反序列化的时间字符串到乔达日期时间一般价值,所以我定义这个解串器:通用类,不能返回

public class JsonTimeDeserializer<T extends DateTime> implements JsonDeserializer<T> { 
    @Override 
    public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { 
     DateTimeFormatter dtf = DateTimeFormat.forPattern("HH:mm"); 
     return json == null ? null : dtf.parseDateTime(json.getAsString()); 
    } 
} 

我越来越:

required: T 
Found org.joda.time.DateTime 

我不明白。 T扩展DateTime。

我在做什么错?

感谢

+0

是什么让你认为'T == DateTime'给出了一个字面上方的行,声明'T扩展DateTime'。事实上,为什么这是通用的? –

+0

好的。我明白你的意思 –

回答

2

你的第一行应为

public class JsonTimeDeserializer implements JsonDeserializer<DateTime> 

你不需要创建自己的变量,只是参考了现有的

当你扩展一个泛型类型,并添加新的类型变量,你也使得你的类型是通用的,这在这种情况下不是你想要的。