总结以下方案,合理选择
方式一:
在实体类,Date字段加注解
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
方式二:配置消息转换器
package com.twj.cloud.module2.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/**
*
* 消息转换器配置
* @author http://blog.csdn.net/phone13144830339
* @date 2018年11月21日
*/
//@Configuration
public class MyFastJsonConfig implements WebMvcConfigurer{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
//config.setCharset(Charset.forName("UTF-8"));//java.nio.charset.Charset
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);//JSON转换编码
config.setDateFormat("yyyy-MM-dd HH:mm:ss");//时间格式化
converter.setSupportedMediaTypes(fastMediaTypes);
converter.setFastJsonConfig(config);
converters.add(0, converter);
}
}
方式三:前端格式化
请百度