Spring boot 当Spring从1.3.X引导到1.4.0时,不一致的org.joda.time.DateTime序列化结果

Spring boot 当Spring从1.3.X引导到1.4.0时,不一致的org.joda.time.DateTime序列化结果,spring-boot,jackson,jodatime,spring-data-rest,Spring Boot,Jackson,Jodatime,Spring Data Rest,在将Spring Boot从1.3.3升级到1.4.0之后,我注意到序列化org.joda.time.DateTime时的行为发生了变化 让我们构建一个简单的SpringBoot+Maven项目 pom.xml依赖项: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId&g

在将Spring Boot从1.3.3升级到1.4.0之后,我注意到序列化org.joda.time.DateTime时的行为发生了变化

让我们构建一个简单的SpringBoot+Maven项目

pom.xml依赖项:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.7.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-joda</artifactId>
    </dependency>
<dependencies>
存储库:

public interface ItemRepository extends MongoRepository<Item, String>{}
这很好

现在,如果我将Spring Boot升级到1.4.0.RELEASE,而不涉及代码库中的任何其他内容,我会得到以下结果:

{
  "_embedded" : {
    "items" : [ {
      "id" : "57bf084a452105f14763cce7",
      "date" : {
        "content" : "1970-01-01T00:00:00.000Z"
      },
      "_links" : {
        "self" : {
          "href" : "http://localhost:8082/items/57bf084a452105f14763cce7"
        },
        "item" : {
          "href" : "http://localhost:8082/items/57bf084a452105f14763cce7"
        }
      }
    } ]
  },
  /* links and metadata omitted */
}
日期时间现在序列化为{“内容”:“1970-01-01T00:00:00.000Z”},而不是“1970-01-01T00:00:00.000Z”

通过使用@JsonFormat注释DateTime字段,可以轻松解决此问题:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
private DateTime date;
实际上,我不喜欢使用@JsonFormat。相反,我使用定制的Jackson2ObjectMapperBuilder(它让我手动或通过@JsonComponent…)处理其他序列化程序和反序列化程序)

坏消息是,我仍然得到同样的结果。我尝试过使用自定义序列化程序,但仍然得到了“content”键。 只有使用com.fasterxml.jackson.databind.ser.std.ToStringSerializer而不是com.fasterxml.jackson.datatype.joda.ser.DateTimeSerializer,我(几乎)才能得到我想要的:

{
  "_embedded" : {
    "items" : [ {
      "id" : "57bf084a452105f14763cce7",
      "date" : "1970-01-01T01:00:00.000+01:00",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8082/items/57bf084a452105f14763cce7"
        },
        "item" : {
          "href" : "http://localhost:8082/items/57bf084a452105f14763cce7"
        }
      }
    } ]
  },
  /* links and metadata omitted */
}

简言之,发生了什么事?那个额外的“内容”键从哪里来,应该在哪里。我想报告这个错误,但我不知道是谁在担心,是Spring Boot的开发人员还是Jackson?

同样的问题,请参见。如果声明了对
Jackson数据类型joda
的依赖关系,那么应该使用
ObjectMapper
自动配置它。Marc概述的问题与我遇到的相同(参见上面的链接),在这种情况下,我明确注册了
JodaModule
,它没有任何效果。这个问题似乎是由于Spring在升级到1.4.0后发生了变化,因为在我们两个案例中,代码都是在升级之前工作的。Spring Boot 1.4.0将JodaTime从2.8.2升级到2.9.4,与Boot 1.3.7相比,Jackson从2.6.7升级到2.8.1。但是,使用raw JodaTime和Jackson可以使用这两个版本中的任何一个给出正确的结果。请参阅此处的测试:@adam单独使用Jackson无法演示该行为。也就是说,我可以用对象映射器序列化同一个对象,而“content”字段不存在。这似乎是REST接口所特有的。正如我在我的帖子中提到的,日期似乎被视为一种资源。@roborative-是的,我想证明raw JodaTime+Jackson工作得很好,只是在春天来临时才搞砸了
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
private DateTime date;
@Configuration
public class JacksonConfiguration {

    @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();

        builder.serializerByType(DateTime.class, new DateTimeSerializer());

        return builder;
    }

}
{
  "_embedded" : {
    "items" : [ {
      "id" : "57bf084a452105f14763cce7",
      "date" : "1970-01-01T01:00:00.000+01:00",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8082/items/57bf084a452105f14763cce7"
        },
        "item" : {
          "href" : "http://localhost:8082/items/57bf084a452105f14763cce7"
        }
      }
    } ]
  },
  /* links and metadata omitted */
}