[alibaba/fastjson]在xml设置全局时间格式后,实体类中设置时间格式无效,始终使用全局格式

2025-11-24 261 views
8

SpringMVC配置

<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
        <property name="fastJsonConfig">
            <bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
                <property name="features">
                    <list>
                        <value>AllowArbitraryCommas</value>
                        <value>AllowUnQuotedFieldNames</value>
                        <value>DisableCircularReferenceDetect</value>
                    </list>
                </property>
                <property name="dateFormat" value="yyyy-MM-dd"></property>
                <property name="serializerFeatures">
                    <list>
                        <value>QuoteFieldNames</value>
                        <value>WriteMapNullValue</value>
                        <value>WriteNullListAsEmpty</value>
                        <value>WriteDateUseDateFormat</value>
                    </list>
                </property>
            </bean>
        </property>
    </bean>

实体:

public class AccountExt {

    @JSONField (format="yyyy-MM-dd HH:mm:ss")
    private Date createDate;

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

跟踪源码 com.alibaba.fastjson.serializer.JSONSerializer类中方法

public final void writeWithFormat(Object object, String format) {
        if(object instanceof Date) {
            // 获取xml配置的时间格式,不为空,为yyyy-MM-dd
            // 传入的format(实体类中配置的yyyy-MM-dd HH:mm:ss),无法使用
            DateFormat dateFormat = this.getDateFormat(); 
            if(dateFormat == null) {
                dateFormat = new SimpleDateFormat(format, this.locale);
                ((DateFormat)dateFormat).setTimeZone(this.timeZone);
            }

            String text = ((DateFormat)dateFormat).format((Date)object);
            this.out.writeString(text);
        } 
      ......
    }

使用版本先后从1.2.24切换到1.2.33

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

请求帮助,是否我有配置不当的地方,导致无法使用实体类中的时间格式。万分感谢!

回答

9

this.getDateFormat(); 取到的是全局dataFormat配置, 这里确实有点问题。

9

目前的设计是toJSONString里的format格式优先于annotation上的format: JSON.toJSONStringWithDateFormat(object, "yyyy"); 或 toJSONString(Object object, // fastJsonHttpMessageConverter用的是这个 SerializeConfig config, // SerializeFilter[] filters, // String dateFormat, // int defaultFeatures, // SerializerFeature... features)

看了一下,com.alibaba.json.bvt.date.DateFieldTest8中的test_0,就是这么设计的。

0

目前看来,以传入参数为准,还是以annotation为准,都存在问题,可以设计一个全局开关来实现切换。

3

已经fixed,测试用例见: src\test\java\com\alibaba\json\bvt\issue_1200\Issue1285.java