背景
- 如今炒的火热的前后端分离项目,大多数开发人员选择RESTful设计风格,Java Web 人员经常要设计 RESTfulAPI,这种设计通常使用 json 数据进行交互。那么前端传入的 json 数据如何序列化成 Java 对象,后端返回的结果又如何将Java 对象解析成 json 格式数据返回给前端。
- spring-boot-stater-web依赖的json解析是jackson,同时也为我们进行了jackson的一系列自动化配置,这样我们不需要导入其他json依赖,就可以直接使用,其中起到关键作用的是
MappingJackson2HttpMessageConverter
问题
Caused by:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
construct instance ofjava.time.ZonedDateTime
(no Creators, like
default construct, exist): no String-argument constructor/factory
method to deserialize from String value
(‘2018-05-26T09:48:31.622+02:00’)
什么意思呢?
就是说在反序列化字符串(‘2018-05-26T09:48:31.622+02:00’)时,没有找到String类型的构造方法,无法构建出java.time.ZonedDateTime
类型对应的实例,抛出了InvalidDefinitionException不合法定义的异常,那么说明框架本身不能解析这种时间格式,需要我们自己去解析
解决方案
方案一:采用jackson的@JsonDeserialize注解
@JsonSerialize(using = CustomZonedTimeSerialize.class)
@JsonDeserialize(using = CustomZonedTimeDeserialize.class)
private ZonedDateTime birth;
这里我们可以根据自己系统的前端架构中时间格式做调整,这里兼容
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd’T’HH:mm:ss.SSS’Z’
yyyy/MM/dd HH:mm:ss
yyyy-MM-dd’T’HH:mm:ss.SSSXXX
这些时间格式
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang.StringUtils;
import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
/**
* 时间 反序列化自定义
*
*
* 兼容以下字符格式的时间串,正确转为ZonedDateTime类型实例
*/
public class CustomZonedTimeDeserialize extends JsonDeserializer<ZonedDateTime> {
private DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
private DateTimeFormatter sdf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneId.systemDefault());
private DateTimeFormatter sdf3 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withZone(ZoneId.systemDefault());
private DateTimeFormatter sdf4 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").withZone(ZoneId.systemDefault());
public CustomZonedTimeDeserialize() {
}
@Override
public ZonedDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ZonedDateTime dateTime = null;
if (StringUtils.isNotBlank(p.getText())) {
if (StringUtils.isNotBlank(p.getText())) {
if (p.getText().indexOf("T") > 0 && p.getText().indexOf("Z") > 0) {
dateTime = ZonedDateTime.parse(p.getText(),sdf2);
}else if (p.getText().indexOf("/") > 0) {
dateTime = ZonedDateTime.parse(p.getText(),sdf3);
} else if (p.getText().indexOf("+") > 0) {
dateTime = ZonedDateTime.parse(p.getText(),sdf4);
} else {
dateTime = ZonedDateTime.parse(p.getText(),sdf);
}
}
}
return dateTime;
}
}