1
1、继承WebMvcConfigurerAdapter后,配置了全局的时间转换格式
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
FastJsonHttpMessageConverter4 jsonConverter = new FastJsonHttpMessageConverter4();
jsonConverter.setFastJsonConfig(fastJsonConfig);
converters.add(0, jsonConverter);
super.extendMessageConverters(converters);
}
2、使用JSONField指定dto转换时间格式
public class RptMtcTimeOrderCountDTO {
@JSONField(format = "yyyy-MM-dd")
private Date statDate;
}
3、最后的转换结果,时间格式为yyyy-MM-dd HH:mm:ss,并不是jsonField的yyyy-MM-dd格式。
4、debug后得到问题在于JSONSerializer类:
public final void writeWithFormat(Object object, String format) {
if (object instanceof Date) {
DateFormat dateFormat = this.getDateFormat();
if (dateFormat == null) {
dateFormat = new SimpleDateFormat(format, locale);
dateFormat.setTimeZone(timeZone);
}
String text = dateFormat.format((Date) object);
out.writeString(text);
return;
}
此处的逻辑判断,应该是先判断参数format是否被配置了时间格式,再判断全局时间格式。