json 时区问题

使用postman 测试时,返回的数据少了8小时。

在调试中,测试数据是正确的,包括接口返回也是正确的。

很明显在转json时出了问题。

先说一下,不要使用jackson来作为json组件。

先说一下,不要使用jackson来作为json组件。

先说一下,不要使用jackson来作为json组件。

(文末,我会说明)

解决方案:

使用了  fastjson

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>

进行如下配置(不要的,自己删除)

package com.tenyears.common.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @description :
 * @auther Tyler
 * @date 2021/8/6
 */
@Configuration
public class FastJSONConfig {
    @Bean
    public HttpMessageConverters httpMessageConverters() {
        HttpMessageConverter<?> fastjson = fastJsonHttpMessageConverter();
        return new HttpMessageConverters(fastjson);
    }

    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter messageConverter = new FastJsonHttpMessageConverter();
        //set MediaTypes
        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
        messageConverter.setSupportedMediaTypes(supportedMediaTypes);

        //fastJsonConfig
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //时区
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.QuoteFieldNames, // 双引号
                SerializerFeature.WriteMapNullValue, // 输入空值字段
                SerializerFeature.WriteEnumUsingToString, // 枚举输出STRING
                SerializerFeature.WriteNullBooleanAsFalse, // 布尔类型如果为null输出false
                SerializerFeature.WriteNullListAsEmpty, // List字段如果为null输出为[]
                //SerializerFeature.WriteNullNumberAsZero, // number类型如果为null输出0
                //SerializerFeature.WriteNullStringAsEmpty, // 字符串类型如果为null输出""
                SerializerFeature.SortField, // 按字段名称排序后进行输出
                SerializerFeature.WriteDateUseDateFormat); // 设置日期格式

        //SerializeConfig
        SerializeConfig config =new SerializeConfig();
        //Long -> String ,前端无法接收Long
        config.put(Long.class , ToStringSerializer.instance);
        config.put(Long.TYPE , ToStringSerializer.instance);
        //BigInteger -> String ,前端无法接收BigInteger
        config.put(BigInteger.class , ToStringSerializer.instance);
        fastJsonConfig.setSerializeConfig(config);

        messageConverter.setFastJsonConfig(fastJsonConfig);
        return messageConverter;
    }
}

最后说一下

为什么不用jackson?

一,配置过jackson的时区问题。

jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: Asia/Shanghai

二,问题

配置jackson后,

postman无法传入 短日期格式  2021-02-03。

必须要  2021-02-03 00:00:00,否则直接bad request!

JSONFormat可以使用timezone属性来指定时区。在引用中的示例中,timezone被设置为"GMT 8",表示使用格林威治标准时间+8小时的时区。在示例中,日期和时间值将以指定的时区格式化和解析。 在引用中的实体类示例中,@JsonFormat注解被用于指定时间字段的格式和时区。time1和time2字段被指定为使用"GMT 8"时区的日期和时间格式。这意味着这两个字段的值将根据"GMT 8"时区进行格式化和解析。 总结起来,JSONFormat可以使用timezone属性来指定日期和时间值的时区。这样可以确保日期和时间的正确解析和格式化,以便在不同的时区之间进行正确的转换。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [@JsonFormat注解默认时间是UTC,不显示指定时区会导致时间看起来少了8小时](https://blog.csdn.net/qq_43657722/article/details/127614838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Jackson(二):@JsonFormat时间格式及时区问题](https://blog.csdn.net/weixin_42717117/article/details/126892612)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值