java JAX-RS Jackson Json Provider 日期格式问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11230954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:18:46  来源:igfitidea点击:

JAX-RS Hymanson Json Provider Date Format Issue

javajsonjax-rsHymansonresteasy

提问by Blessed Geek

WRT to the following question:

回复以下问题:

Jersey + Hymanson JSON date format serialization - how to change the format or use custom HymansonJsonProvider.

Jersey + Hymanson JSON 日期格式序列化 - 如何更改格式或使用自定义 HymansonJsonProvider

I wish to know

我想知道

  • Is Hymanson specifying that the json date format should be normalised to a unix time integer?
  • Hymanson 是否指定应将 json 日期格式规范化为 unix 时间整数?

Follow-up questions ...

后续问题...

  • Has there been a change in its stance anytime recently?
  • Shouldn't a date format be normalised to the same format provided by jaxb xml output?
  • why/why not?
  • any effort put into resolving this issue?
  • has RestEasy provided a json provider mitigation that would output json date in a generally recognisable date format?
  • 它的立场最近是否有任何变化?
  • 日期格式不应该标准化为 jaxb xml 输出提供的相同格式吗?
  • 为什么/为什么不?
  • 有没有努力解决这个问题?
  • RestEasy 是否提供了一个 json 提供程序缓解,可以以普遍可识别的日期格式输出 json 日期?

回答by Blessed Geek

Sorry people for yelling out loud - I found the answers here

对不起,人们大声喊叫 - 我在这里找到了答案

http://wiki.fasterxml.com/HymansonFAQDateHandling,

http://wiki.fasterxml.com/HymansonFAQDateHandling

here

这里

http://wiki.fasterxml.com/HymansonFAQ#Serializing_Dates,

http://wiki.fasterxml.com/HymansonFAQ#Serializing_Dates

here

这里

http://wiki.fasterxml.com/HymansonHowToCustomSerializers

http://wiki.fasterxml.com/HymansonHowToCustomSerializers

here

这里

http://Hymanson.codehaus.org/1.1.2/javadoc/org/codehaus/Hymanson/map/util/StdDateFormat.html

http://Hymanson.codehaus.org/1.1.2/javadoc/org/codehaus/Hymanson/map/util/StdDateFormat.html

Using the @JsonSerialize(using= ... ) way:

使用 @JsonSerialize(using= ... ) 方式:

public class JsonStdDateSerializer
extends JsonSerializer<Date> {
  private static final DateFormat iso8601Format =
    StdDateFormat.getBlueprintISO8601Format();

  @Override
  public void serialize(
    Date date, JsonGenerator jgen, SerializerProvider provider)
  throws IOException, JsonProcessingException {

    // clone because DateFormat is not thread-safe
    DateFormat myformat = (DateFormat) iso8601Format.clone();
    String formattedDate = myformat.format(date);
    jgen.writeString(formattedDate);
  }
}

回答by Ross Judson

This is also controlled by a feature on the ObjectMapper (at least in 1.9.11, and possibly earlier):

这也由 ObjectMapper 上的一个特性控制(至少在 1.9.11 中,可能更早):

ObjectMapper om = new ObjectMapper();
om.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
om.writer().writeValue(System.out, objectWithDateProperty);

I don't see how to declaratively do the equivalent on the object definition itself.

我不知道如何以声明方式对对象定义本身进行等效操作。