Andrew Wanczowski
Andrew Wanczowski

Reputation: 383

OffsetDateTime with Spring Data Couchbase

I am creating a POC using Spring Data Couchbase and running into a slight issue with the new Java 8 DateTime Libraries. I would like my content to be saved and read with ISO-8601 dates. I am using OffsetDateTime to capture the full timestamp with timezone offset. When saving a document I have no issues. However, when reading it from the DB I receive a Mapping Exception. I am using the JSR-310 dependency to configure jackson parsing as well.

Is using OffsetDateTime the right approach? Should I fall back and just use Joda DateTime?

POM Dependencies:

 <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-couchbase</artifactId>
    <version>2.1.1.RELEASE</version>
 </dependency>

 <dependency>
     <groupId>com.fasterxml.jackson.datatype</groupId>
     <artifactId>jackson-datatype-jsr310</artifactId>
     <version>2.6.1</version>
 </dependency>

Document Java Class:

@Document
public class Content {

    @Id
    private Long id;

    @CreatedDate
    private OffsetDateTime createdDate;
...
}

Application YML:

spring:
  jackson:
    deserialization:
      adjust_dates_to_context_time_zone: false
    serialization:
      write_dates_as_timestamps: false
    date-format: yyyy-MM-dd'T'HH:mm:ss.SSSZ

On Save (No Issue):

{
    ...
    "createdDate": "2016-06-14T15:25:27.746-04:00",
    ...
}

On Read (Exception):

{
  "timestamp": "2016-06-14T19:26:08.290+0000",
  "status": 500,
  "error": "Internal Server Error",
  "exception": "org.springframework.data.mapping.model.MappingException",
  "message": "No property null found on entity class java.time.OffsetDateTime to bind constructor parameter to!",
  "path": "/v1/content/a7d078f1-a0d5-4ec1-89f5-149d850aa372"
}

Upvotes: 1

Views: 1627

Answers (1)

Andrew Wanczowski
Andrew Wanczowski

Reputation: 383

Looks like OffsetDateTime and ZoneDateTime are not supported at this time. As a workaround you are able to use Joda DateTime with the following configurations.

POM Dependencies:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-couchbase</artifactId>
  <version>2.1.1.RELEASE</version>
</dependency>
<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.9.4</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-joda</artifactId>
  <version>2.7.4</version>
</dependency>

Document Java Class:

@Document
public class Content {

    @Id
    private Long id;

    @CreatedDate
    private DateTime createdDate;
...
}

Application YML:

spring:
  jackson:
    deserialization:
      adjust_dates_to_context_time_zone: false
    serialization:
      write_dates_as_timestamps: false
    date-format: yyyy-MM-dd'T'HH:mm:ss.SSSZ

On Save:

{
    ...
    "createdDate": "2016-06-14T21:55:23.258+0000",
    ...
}

On Read:

{
    ...
    "createdDate": "2016-06-14T21:55:23.258+0000",
    ...
}

Upvotes: 2

Related Questions