Skip to content

fastjson2兼容包作为springboot默认序列化组件,无法默认序列化Date为时间戳 #2850

@feistel

Description

@feistel

问题描述

简要描述您碰到的问题。
用的fastjson2 兼容包,针对Date类型,直接通过JSON.toJSONString就是默认序列化为时间戳的。

但是通过WebMvcConfigurer 替换springboot默认的jaskson序列化方式的时候,就不生效,直接序列化为yyyy-MM-dd HH:mm:ss 这种形式了
即便在FastJsonConfig设置dateformat= "millis" 也不会生效,请问这种问题该怎么解决呀

环境信息

请填写以下信息:

  • JDK信息: 1.8.0
  • 版本信息:fastjson 2.0.52

重现步骤

import java.nio.charset.StandardCharsets;
import java.util.List;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MessageConverterWebMvcConfigurer implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        // 定义配置信息
        FastJsonConfig config = new FastJsonConfig();
        converter.setFastJsonConfig(config);
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        converters.add(0, converter);
    }
}

期待的正确结果

默认序列化为时间错类型(millis)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions