如何返回具有长值的 Date 对象? 像这样的东西:
@JsonProperty("time")
public Date getTimeInLong() {
Date date = super.getTime();
return date.getTime() //I want this value as date object
}
原因是我试图将 JSON 属性 time
覆盖为长格式。但是,如果我将 getter 签名更改为 public long getTime(){}
,则会收到来自
Conflicting getterDefinitions for property "time":
异常
最佳答案
您可以将带有 shape = Shape.NUMBER
参数的 @JsonFormat
注释放在现有的 getter 方法上。这是一个例子:
public class JacksonJsonFormat {
public static class Bean {
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
public Date getTime() {
return new Date();
}
}
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new Bean()));
}
}
输出:
{"time":1406068019124}
关于Java:长格式的 util.Date 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24867344/