The serialized JSON object has a date in the format 2006-10-04T19: 49: 49. Failed to deserialize it

I use struts2 and serialize the object and pass it to JSP. From jsp, I pass this object to java again and try to deserialize it using the following code

ObjectMapper objectMapper=new ObjectMapper();
receiptDocument = objectMapper.readValue(receiptDocumentStr,new TypeReference<ReceiptDocument>(){});

The object that gets serialized has a property in Timestamp. Therefore, when its serialized date is converted to the following format 2006-10-04T19: 49: 49. But when I try to deserialize it, it gives an exception

org.codehaus.jackson.map.JsonMappingException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

Please suggest me how to do deserialization.

+4
source share
3 answers

Try using as described here

  objectMapper.setDateFormat(myDateFormat);
+1

JSON, jsp struts2,

@Result(name = SUCCESS, type = JSON, params = {
"ignoreHierarchy", "false", "includeProperties","bookList\\[\\d+\\]\\..*})

jquery post , .

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().setDateFormat(sd);
bookListJsonString=  mapper.writeValueAsString(bookList);

ObjectMapper objectMapper=new ObjectMapper();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
objectMapper.getDeserializationConfig().setDateFormat(sd);
objectMapper.getDeserializationConfig().disable(Feature.FAIL_ON_UNKNOWN_PROPERTIES); //To avoid failure if there is no any class fields.                
List<BooksDocument> bookList =  objectMapper.readValue(bookListJsonString, new TypeReference<List<BooksDocument>>() {});

- . , .

0

jackson org.codehaus.jackson. Jackson. com.fasterxml.jackson - jackson.

.

https://github.com/FasterXML/jackson

, - . .

.

0

Source: https://habr.com/ru/post/1665496/


All Articles