Java Jackson时间戳反序列化错误
我有一个自定义的objectmapper类:Java Jackson时间戳反序列化错误,java,json,jackson,timestamp,deserialization,Java,Json,Jackson,Timestamp,Deserialization,我有一个自定义的objectmapper类: import java.text.DateFormat; import java.text.SimpleDateFormat; import org.codehaus.jackson.map.ObjectMapper; public class CustomObjectMapper extends ObjectMapper { public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.codehaus.jackson.map.ObjectMapper;
public class CustomObjectMapper extends ObjectMapper {
public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
public CustomObjectMapper() {
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
this.setDateFormat(df);
}
和单元测试:
@Test
public void testSerialization() throws JsonParseException, JsonMappingException, IOException {
String timestamp = "2019-02-12T07:53:11+0000";
CustomObjectMapper customObjectMapper = new CustomObjectMapper();
Timestamp result = customObjectMapper.readValue(timestamp, Timestamp.class);
System.out.println(result.getTime());
}
junit测试给了我“2019”
我尝试使用CustomTimestames反序列化程序:
public class CustomJsonTimestampDeserializer extends
JsonDeserializer<Timestamp> {
@Override
public Timestamp deserialize(JsonParser jsonparser,
DeserializationContext deserializationcontext) throws IOException,
JsonProcessingException {
String date = jsonparser.getText(); //date is "2019"
JsonToken token = jsonparser.getCurrentToken(); // is JsonToken.VALUE_NUMBER_INT
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
CustomObjectMapper.DATE_FORMAT);
try {
return new Timestamp(simpleDateFormat.parse(date).getTime());
} catch (ParseException e) {
return null;
}
}
公共类CustomJsonTimestampDeserializer扩展
JsonDeserializer{
@凌驾
公共时间戳反序列化(JsonParser JsonParser,
反序列化上下文(反序列化上下文)引发IOException,
JsonProcessingException{
String date=jsonparser.getText();//日期为“2019”
JsonToken token=jsonparser.getCurrentToken();//是JsonToken.VALUE\u NUMBER\u INT
SimpleDataFormat SimpleDataFormat=新SimpleDataFormat(
CustomObjectMapper.DATE_格式);
试一试{
返回新的时间戳(simpleDataFormat.parse(date.getTime());
}捕获(解析异常){
返回null;
}
}
}
我做错了什么?jackson似乎认为时间戳字符串是一个整数,并在2019年后停止对其进行解析。可能是jackson没有将时间戳识别为日期类型,因此不依赖日期格式
您能否尝试使用
Java.util.Date
而不是时间戳
这种方法有两个问题
首先,有一个可疑的进口声明:
import org.codehaus.jackson.map.ObjectMapper;
org.codehaus
是当前com.fasterxml
的前身。不清楚它是否是故意使用的,但是ObjectMapper
的导入应该是
import com.fasterxml.jackson.databind.ObjectMapper;
其次,时间戳不能直接从这样的普通字符串中读取
String timestamp = "2019-02-12T07:53:11+0000";
ObjectMapper
需要一个JSON字符串。如果是的话
{ "timestamp": "2019-02-12T07:53:11+0000" }
还有包装课
class TimestampWrapper {
private Timestamp timestamp;
// getter + setter for timestamp
}
然后测试序列将正确执行:
String timestamp = "{ \"timestamp\": \"2019-02-12T07:53:11+0000\" }";
CustomObjectMapper customObjectMapper = new CustomObjectMapper();
TimestampWrapper result = customObjectMapper.readValue(timestamp, TimestampWrapper.class);
System.out.println(result.getTimestamp());
更新:
或者,在不使用专用包装器类的情况下,可以从JSON数组对其进行反序列化:
String timestamp = "[ \"2019-02-12T07:53:11+0000\" ]";
CustomObjectMapper customObjectMapper = new CustomObjectMapper();
Timestamp[] result = customObjectMapper.readValue(timestamp, Timestamp[].class);
System.out.println(result[0]);
您是否使用
java.sql
中的Timestamp
?我建议您不要使用SimpleDateFormat
和Timestamp
。这些类设计得很糟糕,而且早已过时,其中前者尤其令人讨厌。改为使用来自的Instant
from.{“timestamp”:“2019-02-12T07:53:11+0000”}