matrix.boot.based.serializer.DateTimeDeserializer Maven / Gradle / Ivy
package matrix.boot.based.serializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import matrix.boot.common.exception.ServiceException;
import matrix.boot.common.utils.DateUtil;
import matrix.boot.common.utils.StringUtil;
import java.io.IOException;
import java.util.Date;
/**
* 反序列化Date类型为string
*
* @author wangcheng
*/
public class DateTimeDeserializer extends JsonDeserializer {
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
String date = jsonParser.getText();
if (StringUtil.isEmpty(date)) {
return null;
}
try {
return DateUtil.parse(DateUtil.STANDARD_FORMATTER, date);
} catch (Exception e) {
throw new ServiceException(e);
}
}
}