Json RequestBody日期相差8小时及首字母大写问题

数据库和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")

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值