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

使用JsonField配置日期格式,被FastJsonConfig的全局配置覆盖掉 #1868

Open
201314 opened this issue May 23, 2018 · 7 comments · May be fixed by #2228
Open

使用JsonField配置日期格式,被FastJsonConfig的全局配置覆盖掉 #1868

201314 opened this issue May 23, 2018 · 7 comments · May be fixed by #2228

Comments

@201314
Copy link

201314 commented May 23, 2018

1、继承WebMvcConfigurerAdapter后,配置了全局的时间转换格式

	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		FastJsonHttpMessageConverter4 jsonConverter = new FastJsonHttpMessageConverter4();
		jsonConverter.setFastJsonConfig(fastJsonConfig);
		converters.add(0, jsonConverter);
		super.extendMessageConverters(converters);
	}

2、使用JSONField指定dto转换时间格式

public class RptMtcTimeOrderCountDTO {
	@JSONField(format = "yyyy-MM-dd")
	private Date statDate;
}

3、最后的转换结果,时间格式为yyyy-MM-dd HH:mm:ss,并不是jsonField的yyyy-MM-dd格式。

4、debug后得到问题在于JSONSerializer类:

    public final void writeWithFormat(Object object, String format) {
        if (object instanceof Date) {
            DateFormat dateFormat = this.getDateFormat();
            if (dateFormat == null) {
                dateFormat = new SimpleDateFormat(format, locale);
                dateFormat.setTimeZone(timeZone);
            }
            String text = dateFormat.format((Date) object);
            out.writeString(text);
            return;
        }

此处的逻辑判断,应该是先判断参数format是否被配置了时间格式,再判断全局时间格式。

@zxk175
Copy link

zxk175 commented Jun 2, 2018

image
这样可以解决

@201314
Copy link
Author

201314 commented Jun 8, 2018

@zxk175 感觉这样有点取巧

@201314
Copy link
Author

201314 commented Jun 12, 2018

@zxk175 这样操作,全局的配置无法生效,直接给返回long类型。还要每个去配置,贼麻烦。

@cxy376700398
Copy link

我也遇到这个问题了。format值传进去了,但是写死了,优先使用全局设置的dateFormat。很蛋疼。

@folochina
Copy link

这个问题应该改吧,正常来说都是从小到大生效的配置

@folochina
Copy link

image
这样可以解决

默认就是这个内容 不过spring mvc貌似无效,还是long返回。须每个时间都加@JsonField了,

@jqncc
Copy link

jqncc commented Aug 7, 2019

问题解决了吗,感觉很久了啊,现在版本还是有

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants