JAX-RS Jackson Json Provider Date Format Issue

倖福魔咒の 提交于 2019-12-04 23:02:07

问题


WRT to the following question:

Jersey + Jackson JSON date format serialization - how to change the format or use custom JacksonJsonProvider.

I wish to know

  • Is Jackson specifying that the json date format should be normalised to a unix time integer?

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?

回答1:


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

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

here

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

here

http://wiki.fasterxml.com/JacksonHowToCustomSerializers

here

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

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

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);
  }
}



回答2:


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

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.



来源:https://stackoverflow.com/questions/11230954/jax-rs-jackson-json-provider-date-format-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!