当前位置: 首页 > 知识库问答 >
问题:

与Jackson和Spring一起序列Joda DateTime

韦寒
2023-03-14

我在使用Spring Boot和Jackson-databind 2.5.2将Joda DateTime从java序列化和反序列化到json时遇到了问题。我的pom.xml是这样的。

    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.2</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>2.5.2</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.2.1.RELEASE</version>
</dependency>

当我序列化日期时间对象时,我得到一个表示日期时间的整数。实际上不是我所期望的,但是很好。但是当我保存我的对象时,我得到了下面的错误...

Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'endTime';
nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type org.joda.time.DateTime for value '1428600998511'

出于某种原因,它将其序列化为整数,然后将其反序列化为字符串,但事实并非如此。我还尝试在调用rest服务之前设置endTime=new Date(intValue),但尝试将诸如“Tue Apr 28 2015 00:00:00 GMT-0700(PDT)”之类的字符串转换为DateTime也失败了。

我做错了什么?

更新:

这是我得到的JSON,我试图立即POST回来。

{
    id: 4,
    username: "",
    name: "eau",
    email: "aoue",
    verbatimLocation: null,
    latitude: null,
    longitude: null,
    startTime:null,
    endTime: 1429034332312,
    description: "ueoa",
    media: [ ],
    timeSubmitted: 1428600998000,
    status: null,
    submissionid: null
}

共有2个答案

吕冠宇
2023-03-14

最后我能够按照beerbajay说的去做,用...

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSSZ")

...连载我的约会。不过,我最终还是使用了Long而不是DateTime,因为在javascript端处理日期太麻烦了。对于我有限的时间来说,找到一个适用于jquery datepicker、joda DateTime和postgresql的模式被证明是太多的工作了。

仇龙光
2023-03-14

为了获得更高的可重用性,您可以创建一个< code>JsonSerializer:

/**
 * When passing JSON around, it's good to use a standard text representation of
 * the date, rather than the full details of a Joda DateTime object. Therefore,
 * this will serialize the value to the ISO-8601 standard:
 * <pre>yyyy-MM-dd'T'HH:mm:ss.SSSZ</pre>
 * This can then be parsed by a JavaScript library such as moment.js.
 */
public class JsonJodaDateTimeSerializer extends JsonSerializer<DateTime> {

    private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime();

    @Override
    public void serialize(DateTime value, JsonGenerator gen, SerializerProvider arg2)
            throws IOException, JsonProcessingException {

        gen.writeString(formatter.print(value));
    }

}

然后,您可以使用以下命令对 get 方法进行批注:

@JsonSerialize(using = JsonJodaDateTimeSerializer.class)

这在整个应用程序中为您提供了一致的格式,而无需到处重复文本模式。它还具有时区意识。

 类似资料:
  • 我有一个简单的枚举,我想序列化和反序列化。该类如下所示: 问题是,我通过restful调用发送它,接收方可以接收任何类型(因此它只知道它将接收对象)。所以Jackson应该能够找出反序列化它的参数类型。 有可能这样做吗?我认为在生成的json中包含类名应该可以让Jackson判断出类型,但我一直无法做到这一点。

  • 我刚刚开始尝试Scala,并尝试在现有的Java/Spring应用程序中使用Jackson2.3.1和Jackson-module-scala。 我试图将JSON反序列化,如下面所示,以到对象的结束,但当我运行下面的代码时,我以到结束: 为什么结果地图的值是另一个地图而不是颜色对象? 是一个具有注释构造函数的现有Java类。 将上述JSON传递给这段代码的输出如下:

  • 对于某些用例,我需要将一个POJO转换为另一个具有不同字段名称的POJO。我试着使用Jackson对象映射器。它在某些方面起了作用。然而,最终的结果并不是我所期望的。 如果运行,输出将是: SOUT:UserMap{name_new='Deepak',meta=address::Singapore,id::111} 杰克逊:{“姓名”:“迪帕克”,“地址”:“新加坡”,“身份证”:“111”} 我

  • 我尝试调用一个url谁接受列表。 发送的数据为 “{”时间戳“:1445958336633,”状态“:400,”错误“:”错误请求“,”异常“:”org.springframework.http.converter.httpmessagenotreadableException“,”消息“:”无法读取文档:无法反序列化START_OBJECT令牌之外的java.util.arraylist实例\n

  • 我有两个问题合二为一: 首先,我正在尝试使用Junit4测试请愿书后,我有这样的东西: 错误: java.lang.ClassCastException: class com.fasterxml.jackson.databind.node.ArrayNode 不能 cast to class com.fasterxml.jackson.databind.node.ObjectNode (com.f

  • 我正在尝试使用spring data redis的Jackson序列化功能。我正在构建一个ObjectMapper,并使用GenericJackson2JsonRedisSerializer作为redisTemplate的序列化程序: 我正试图保存一个样本bean: 以及该bean的存储库: 然后我尝试将bean写入Redis: 我希望redisTemplate使用序列化程序将SampleBean