本文介绍了@JsonFormat DEFAULT_TIMEZONE似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在隔离到JSON序列化器Jackson的日期逻辑上遇到了一些问题.

I am having some issue with date logic which I've isolated to Jackson, the JSON serializer.

在数据库和应用程序的调试点中,日期正确,并且所有内容均使用默认时区编写.但是,在序列化中要添加4个小时.我发现可以通过告诉杰克逊专门使用EST(它默认为UTC)来补救.如下:

In the database and in a debug point in the application, dates are correct and everything is written using default timezone. However, in serialization 4 hours are being added. I found this could be remedied by telling Jackson specifically to use EST (it was defaulting to UTC). As below:

@JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss.SSSZ", timezone="America/New_York")
private Date startDate;

但是,问题是只有本地使用EST,而服务器将使用UTC.我需要杰克逊使用系统默认设置.

However, the problem is that only local is using EST and the server will be using UTC. I need Jackson to use system defaults.

幸运的是,我发现了这个类似的问题得到了备份通过文档 .新解决方案:

Luckily, I found this similar question which is backed up by the documentation. New solution:

@JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss.SSSZ", timezone=JsonFormat.DEFAULT_TIMEZONE)
private Date startDate;

但是,它不起作用!我也尝试了timezone='DEFAULT_TIMEZONE'和其他各种方法,但是在所有情况下,api的输出似乎仍然比数据库中的数字早4小时.

However, it doesn't work! I tried also timezone='DEFAULT_TIMEZONE' and a variety of other things but in all cases the api output still seems to be 4 hours ahead of the number in the database.

我尝试过的其他事情:

  • 注销JsonFormat.DEFAULT_TIMEZONE会返回##default.
  • 记录TimeZone.getDefault().getDisplayName()返回Eastern Standard Time.
  • logging out JsonFormat.DEFAULT_TIMEZONE returns ##default.
  • logging TimeZone.getDefault().getDisplayName() returns Eastern Standard Time.

Jackson版本是2.9.

Jackson version is 2.9.

有什么建议吗?

推荐答案

解决了我自己的问题.这是我发现的:

Solved my own question. Here is what I found:

JsonFormat.DEFAULT_TIMEZONE不是系统默认值,因为文档 SO答案建议,但实际上默认为UTC.

JsonFormat.DEFAULT_TIMEZONE is NOT the system default, as the documentation and SO answer suggest, but actually defaults to UTC.

org.springframework.http.converter.json.Jackson2ObjectMapperBuilder

/**
 * Override the default {@link TimeZone} to use for formatting.
 * Default value used is UTC (NOT local timezone).
 * @since 4.1.5
 */
public Jackson2ObjectMapperBuilder timeZone(TimeZone timeZone) {

com.fasterxml.jackson.annotation.JsonFormat

/**
 * Value that indicates that default {@link java.util.TimeZone}
 * (from deserialization or serialization context) should be used:
 * annotation does not define value to use.
 *<p>
 * NOTE: default here does NOT mean JVM defaults but Jackson databindings
 * default, usually UTC, but may be changed on <code>ObjectMapper</code>.
 */
public final static String DEFAULT_TIMEZONE = "##default";

解决方案:

@Autowired
com.fasterxml.jackson.databind.ObjectMapper objectMapper;

objectMapper.setTimeZone(TimeZone.getDefault());

这应将Jackson ObjectMapper设置为使用系统默认值而不是Jackson默认值(UTC).

This should set the Jackson ObjectMapper to use system default instead of Jackson default (UTC).

这篇关于@JsonFormat DEFAULT_TIMEZONE似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:33