数据库和postman相差8个小时,debug后发现是jackson得问题。
如果要解决这种问题,用文件配置可以解决
#application.properties文件配置
spring.jackson.time-zone=GMT+8
------------------------------------
#application.yml文件配置
spring:
jackson:
time-zone: GMT+8
或者这样也可以解决,在你的时间上设置时间格式化。
public class Vo {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date createTime;
}
二、设置为FasJson
如果想SpringBoot默认使用FastJson,一般有两种方式:
方式一、启动类继承WebMvcConfigurerAdapter ,复写configureMessageConverters
/**
* 在这里我们使用@SpringBootApplication指定这是一个 spring boot的应用程序.
*/
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter {
// 第一种方式配置使用FstJson
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
方式二、添加Bean到Spring容器,进行管理
package com.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
/**
* 设置config类 使用 @Bean注入 fastJsonHttpMessageConvert
*/
@Configuration
public class MassageConverConfiguration {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
-
二、首字母大小问题
MappingJackson2HttpMessageConverter
jackson
日期格式:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
首字母大写: @JsonProperty("Age") private int Age;
fastjson:
日期格式, 首字母大写:
@JSONField(name = "Age", format = "yyyy-MM-dd HH:mm:ss") private Integer age;
gson:
日期格式:Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
System.out.println(gson.toJson(user));
首字母大写: @SerializedName("Age")