简体   繁体   English

使用@JsonCreator和d @JsonProperty未能按预期生成Json响应

[英]Json response not generated as expected using @JsonCreator andd @JsonProperty

I am using JDK 1.6 , Jackson 2.0.5 , Spring 3.0.3 and using jackson annotations to serialize and deserialize json response. 我正在使用JDK 1.6,Jackson 2.0.5,Spring 3.0.3并使用jackson批注对JSON响应进行序列化和反序列化。

public class MyDate extends java.sql.Date{

  private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");
  private String dateString;

  @JsonCreator
  public MyDate(@JsonProperty("dateString") String date){
        super(simpleDateFormat.parse(date).getTime());
        dateString = date;
  }

  public String getDateString(){
        return dateString;
  }
  private final setDateString(String date){
        // .....
  }
}

I am getting json response message like 1417977000000 , but it should be like 12/08/2014. 我收到json响应消息,例如1417977000000,但应该像2014年12月8日。 Do I need to write anything else also ? 我还需要写其他东西吗?

I tried to follow Jackson wiki page article. 我试图关注Jackson维基页面文章。

EDIT: 编辑:

On basis of discussion with @Sotirios Delimanolis I tried JsonValue annotation on getter method and it worked. 在与@Sotirios Delimanolis的讨论的基础上,我在getter方法上尝试了JsonValue注释,并且该注释有效。

  @JsonValue
  public String getDateString(){
        return dateString;
  }

Your type is a java.sql.Date which is a java.util.Date which is considered special for Jackson. 您的类型是java.sql.Date ,它是java.util.Date ,对于Jackson来说是特殊的。 By default, it writes dates (instances of type java.util.Date ) as their timestamps. 默认情况下,它会将日期(类型为java.util.Date实例)写入其时间戳。 You can disable this with 您可以使用

objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

or the corresponding configuration for your version of Jackson. 或您的Jackson版本的相应配置。

This, however, will give you something like 但是,这会给你类似的东西

"2014-12-08T08:00:00.000+0000"

It will not use your SimpleDateFormat . 它不会使用您的SimpleDateFormat You can register a DateFormat with the ObjectMapper like so 您可以像这样用ObjectMapper注册DateFormat

objectMapper.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));

The best solution would be to use composition over inheritance. 最好的解决方案是使用组合而不是继承。 Do not extend java.util.Date directly or indirectly. 不要直接或间接扩展java.util.Date

If you don't want to use a general solution, you can always set format per property 如果您不想使用一般解决方案,则始终可以按属性设置格式

public class DateStuff {
     @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd,HH:00", timezone="CET")
     public Date creationTime;
}

For more information please read here: http://wiki.fasterxml.com/JacksonFAQDateHandling 有关更多信息,请在这里阅读: http : //wiki.fasterxml.com/JacksonFAQDateHandling

Edit: 编辑:

As a side note 作为旁注

(aka "Please do NOT use java.sql.Date, ever!") (又名“请永远不要使用java.sql.Date!”)

Although Jackson supports java.sql.Date, there are known issues with respect to timezone handling, partly due to design of this class. 尽管Jackson支持java.sql.Date,但在时区处理方面存在一些已知问题,部分原因是此类的设计。 It is recommended that this type is avoided, if possible, and regular java.util.Date (or java.util.Calendar) used instead. 如果可能的话,建议避免使用这种类型,而应使用常规java.util.Date(或java.util.Calendar)代替。 If this is not possible, it may be necessary for applications to convert these dates using java.util.Calendar and explicit timezone definition. 如果这不可能,则应用程序可能有必要使用java.util.Calendar和显式的时区定义来转换这些日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM