一、一般情况下前端要求的时间格式不固定,有时需要时分秒,有时不需要,无法统一,但是后端如果按时间格式(yyyy-MM-dd HH:mm:ss)返回前端,就需要前端分别处理,比较麻烦。
最好就是直接给前端返回时间戳,让前端通过时间工具获取。
二、后端在接收前端传入的时间时,一般都是按 yyyy-MM-dd HH:mm:ss 或者 yyyy-MM-dd 的格式去接收。
这就导致入参和出参的时间格式不一致
三、解决办法
首先处理 springboot 的时间格式
application.properties配置
spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
实现方法
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;
@Configuration
@Slf4j
public class WebConf extends WebMvcConfigurationSupport {
//从配置文件中获取
@Value("${spring.jackson.date-format}")
private String dateFormatPattern;
@Value("${spring.jackson.time-zone}")
private String timeZone;
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = converter.getObjectMapper();
// 生成JSON时,将所有Long转换成String
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
// 时间格式化
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 这个可以引用spring boot yml 里的格式化配置和时区配置
objectMapper.setDateFormat(new SimpleDateFormat(dateFormatPattern));
objectMapper.setTimeZone(TimeZone.getTimeZone(timeZone));
// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);//过滤 null
// 设置格式化内容
converter.setObjectMapper(objectMapper);
converters.add(0, converter);
super.extendMessageConverters(converters);
}
}
这时,springboot 的所有时间都是 yyyy-MM-dd HH:mm:ss 格式的,但是要求返回的是时间戳,需要重写 JsonSerializer<Date> 下的 serialize方法
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.Date;
/**
* @author
* @ClassName:
* @Description: 设置返回的时间格式
* @date
*/
public class TimeStamp extends JsonSerializer<Date> {
@Override
public void serialize(Date date, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeNumber(date.getTime());
}
}
在实体类上添加注解
@JsonSerialize(using= TimeStamp.class)
实体类实例
import java.util.Date;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.tech.jzwm.manager.config.security.TimeStamp;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 用户表(Userinfo)实体类
*
* @author makejava
* @Date 2021-04-12 17:28:34
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Userinfo {
private static final long serialVersionUID = -78629171783478001L;
@ApiModelProperty(value = "用户id", name = "userId")
private Long userId;
@ApiModelProperty(value = "用户名", name = "name")
private String name;
@ApiModelProperty(value = "用户密码", name = "password")
private String password;
@ApiModelProperty(value = "删除标记 0未删除 1已删除", name = "delFlag")
private Integer delFlag;
@ApiModelProperty(value = "创建人", name = "createUser")
private Long createUser;
@JsonSerialize(using= TimeStamp.class)
@ApiModelProperty(value = "创建时间", name = "createTime")
private Date createTime;
@ApiModelProperty(value = "修改人", name = "updateUser")
private Long updateUser;
@JsonSerialize(using= TimeStamp.class)
@ApiModelProperty(value = "修改时间", name = "updateTime")
private Date updateTime;
}
这时,接收入参时,时间格式为正常的时间格式,出参中的时间则是时间戳的