Java 在Spring4MVC中将Json字符串日期转换为ZoneDateTime
我可以通过调用SpringRESTAPI获得ZoneDateTime。我在json中获得的日期格式如下:Java 在Spring4MVC中将Json字符串日期转换为ZoneDateTime,java,json,spring-mvc,Java,Json,Spring Mvc,我可以通过调用SpringRESTAPI获得ZoneDateTime。我在json中获得的日期格式如下: { "2017-04-24T15:13:06-05:00" } 通过在ApplicationConfiguration.class中配置以下代码,我在Spring 4 MVC中实现了这一点: @Override public void configureMessageConverters(List<HttpMessageConverter<?>>
{
"2017-04-24T15:13:06-05:00"
}
通过在ApplicationConfiguration.class中配置以下代码,我在Spring 4 MVC中实现了这一点:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.registerModule(new JavaTimeModule());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper);
converters.add(converter);
}
我尝试使用CustomDeserialization.class并用@jsondeSerialization(CustomDeserialization.class)注释ZoneDateTime字段,但这也不起作用
在Spring4MVC中,将具有日期的json转换为ZoneDateTime的最佳方式是什么 如果您想使用JSON发送日期,我认为最简单的方法是首先将日期转换为Long类型,然后再将其作为JSON发送。大概是这样的:
public class MyJson {
Long date;
public MyJson() {
}
public MyJson(Long date) {
this.date = date;
}
public Long getDate() {
return date;
}
public void setDate(Long date) {
this.date = date;
}
}
关于主要方法:
Date date = new Date();
MyJson json = new MyJson(date.getTime());
ObjectMapper objectMapper = new ObjectMapper();
String strJson = objectMapper.writeValueAsString(json);
MyJson result = objectMapper.readValue(strJson, MyJson.class);
首先应该是什么区域?有多个时区的偏移量为-05:00。应该选择哪一个?为什么要将看起来不像分区日期时间的内容反序列化为分区日期时间?我在entity类中有一个createdDate字段,它作为ZoneDateTime存储在数据库中。我只想发送ZoneDateTime的json字符串并将其存储在数据库中。通过使用ObjectMapper,我可以从数据库中获取ZoneDateTime,但无法使用post请求发送。
Date date = new Date();
MyJson json = new MyJson(date.getTime());
ObjectMapper objectMapper = new ObjectMapper();
String strJson = objectMapper.writeValueAsString(json);
MyJson result = objectMapper.readValue(strJson, MyJson.class);