alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] FastJsonHttpMessageConverter 转换器对时间字段(Date型)格式化失效

superzjt526 opened this issue · comments

问题描述

后端fastjson统一转换器(统一格式化对象中的时间字段值),在1.2.83版本中有效,在2.0.49、2.0.50中无效。

有效时前端收到时间信息:“2024-05-14 21:31:10”
失效时前端收到时间信息:1715693470343

(部分关键源代码看下方)

环境信息

请填写以下信息:

  • OS信息: Win10
  • JDK信息: JDK 1.8.0_221
  • 版本信息:Fastjson2 2.0.50

重现步骤

如何操作可以重现该问题:

  1. 使用 WebMvcConfigurer 自定义消息转换器(configureMessageConverters),其中使用 fastjson 转换器
  2. Controller 返回 Object 或 List
  3. 时间转换失效
  4. import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import com.yunhesoft.core.utils.AnnotationClassScanner;
    import com.yunhesoft.core.utils.StringUtils;
    import com.yunhesoft.system.auth.service.AuthService;
    import com.yunhesoft.system.kernel.settings.ConstSettings;
    import com.yunhesoft.system.kernel.settings.ConstSettingsInterface;
    import com.yunhesoft.system.tools.sysConfig.service.ISysConfigService;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import javax.servlet.MultipartConfigElement;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.web.servlet.MultipartConfigFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.util.unit.DataSize;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class Tm4WebMvcConfigurer implements WebMvcConfigurer {
        //... 非关键代码这里不列了
    
        @Bean
        public HttpMessageConverter<?> fastJsonHttpMessageConverters() {
            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(new SerializerFeature[]{SerializerFeature.PrettyFormat, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue});
            fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
            fastConverter.setFastJsonConfig(fastJsonConfig);
            HttpMessageConverter<?> converter = fastConverter;
            return converter;
        }
    
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(this.fastJsonHttpMessageConverters());
        }
    }

    期待的正确结果

    在不使用字段注解与 JSON.toJSONString 方法的前提下使用 fastjson 转换器,能够正常执行 MVC 框架消息转换返回正确时间字符串给前端

    相关日志输出

    附加信息