Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
superzjt526 opened this issue May 15, 2024 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@superzjt526
Copy link

superzjt526 commented May 15, 2024

问题描述

后端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 框架消息转换返回正确时间字符串给前端

    相关日志输出

    附加信息

@superzjt526 superzjt526 added the bug Something isn't working label May 15, 2024
@wenshao wenshao added this to the 2.0.51 milestone May 17, 2024
@wenshao
Copy link
Member

wenshao commented May 22, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.51-SNAPSHOT/
问题已修复,请帮忙用2.0.51-SNAPSHOT版本验证下

@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.51
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants