5

I am trying to set up a Spring Boot application with a MongoDB database. Here is an excerpt from the dependencies I have (in Gradle representation).

compile("org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.5.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-data-mongodb:1.5.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-hateoas:1.5.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-security:1.5.1.RELEASE")
compile("org.springframework.security:spring-security-test:1.5.1.RELEASE)
testCompile("org.springframework.boot:spring-boot-starter-test:1.5.1.RELEASE")
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.8")

My @Document annotated Java class contains a OffsetDateTime attribute.

@Document(collection = "reports")
public class ReportDocument implements Serializable {

    @Id private String id;
    @Version private Long version;
    //...
    private OffsetDateTime start;
    private OffsetDateTime end;
    //...
}

When I call a REST-Controller that retrieves these Documents, it fails with an exception

org.springframework.data.mapping.model.MappingException: No property null found on entity class java.time.OffsetDateTime to bind constructor parameter to!
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:74) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:63) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:71) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:83) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:257) ~[spring-data-mongodb-1.10.0.RELEASE.jar:na]

I read a lot of forums. Some people replaced the OffsetDateTime with the Joda-libraries DateTime. That's not the way to go for me, since Joda states to use the Java 8 DateTime-Types.

What am I doing wrong (I know the problem is always in front of the computer) and how can I solve it? Anyone any idea about this?

UPDATE (from Apr'22/2017) I did like @Veeram said and updated my application with the Converters (Date -> OffsetDateTime and vice versa).

package com.my.personal.app.converter;

import org.springframework.core.convert.converter.Converter;

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Date;

public class DateToOffsetDateTimeConverter implements Converter<Date, OffsetDateTime> {

    @Override
    public OffsetDateTime convert(Date source) {
        return source == null ? null : OffsetDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
    }

}

and

package com.my.personal.app.converter;

import org.springframework.core.convert.converter.Converter;

import java.time.OffsetDateTime;
import java.util.Date;

public class OffsetDateTimeToDateConverter implements Converter<OffsetDateTime, Date> {

    @Override
    public Date convert(OffsetDateTime source) {
        return source == null ? null : Date.from(source.toInstant());
    }

}

registering the converters

package com.my.personal.app;

import com.my.personal.app.converter.DateToOffsetDateTimeConverter;
import com.my.personal.app.converter.OffsetDateTimeToDateConverter;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class MongoConfig extends AbstractMongoConfiguration {


    @Override
    protected String getDatabaseName() {
        return "my-personal-database";
    }

    @Override
    public Mongo mongo() throws Exception {
        return new MongoClient("localhost");
    }

    @Bean
    @Override
    public CustomConversions customConversions() {
        List<Converter<?, ?>> converterList = new ArrayList<Converter<?, ?>>();
        converterList.add(new DateToOffsetDateTimeConverter());
        converterList.add(new OffsetDateTimeToDateConverter());
        return new CustomConversions(converterList);
    }

    @Bean
    @Override
    public MongoTemplate mongoTemplate() throws Exception {
        MappingMongoConverter converter = new MappingMongoConverter(
                new DefaultDbRefResolver(mongoDbFactory()), new MongoMappingContext());
        converter.setCustomConversions(customConversions());
        converter.afterPropertiesSet();
        return new MongoTemplate(mongoDbFactory(), converter);
    }


}

but again resulting in the exception

org.springframework.data.mapping.model.MappingException: No property null found on entity class java.time.OffsetDateTime to bind constructor parameter to!
    at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:74) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:63) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:71) ~[spring-data-commons-1.13.0.RELEASE.jar:na]

Am I missing or misunderstanding sth. or doing sth. wrong?

UPDATE of my documents in the collection

Here is an excerpt with the essential parts of my collection's documents

[
  {
    "_id": {
      "$oid": "58f8b107affb5f08e0a78a96"
    },
    "_class": "com.my.personal.app.document.ReportDocument",
    "version": 0,
    "checklistId": 2,
    "vehicleGuid": "some-vehicle-guid",
    "userGuid": "some-user-guid",
    "name": "Report 123",
    "start": {
      "dateTime": {
        "$date": "2017-04-20T12:00:55.930Z"
      },
      "offset": "+02:00"
    },
    "stations": [
      {
        "_id": 1,
        "name": "Front"
      }
    ]
  },
  {
    "_id": {
      "$oid": "58f8bf78affb5f2dec896acf"
    },
    "_class": "com.my.personal.app.document.ReportDocument",
    "version": 0,
    "checklistId": 2,
    "vehicleGuid": "some-vehicle-guid",
    "userGuid": "some-user-guid",
    "name": "Report 123",
    "start": {
      "dateTime": {
        "$date": "2017-04-20T10:02:32.930Z"
      },
      "offset": "+02:00"
    },
    "stations": [
      {
        "_id": 1,
        "name": "Front"
      }
    ]
  }
]

This is the REST controller which tries to call the documents

@RequestMapping(value = "/mongoreports")
public class MongoReportController {

    @Autowired
    private MongoReportRepository repository;

    @RequestMapping(
            method = RequestMethod.GET,
            produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<List<ReportDocument>> show(
            @RequestParam(name = "vehicleGuid") Optional<String> vehicleGuid,
            @RequestParam(name = "userGuid") Optional<String> userGuid) {
        if (vehicleGuid.isPresent() && !userGuid.isPresent()) {
            List<ReportDocument> reportDocuments = repository.findByVehicleGuidOrderByStartAsc(vehicleGuid.get());
            return ResponseEntity.ok(reportDocuments);
        }
        if (!vehicleGuid.isPresent() && userGuid.isPresent()) {
            List<ReportDocument> reportDocuments = repository.findByUserGuidOrderByStartAsc(userGuid.get());
            return ResponseEntity.ok(reportDocuments);
        }
        if (vehicleGuid.isPresent() && userGuid.isPresent()) {
            List<ReportDocument> reportDocuments = repository.findByUserGuidAndVehicleGuidOrderByStartAsc(vehicleGuid.get(), userGuid.get());
            return ResponseEntity.ok(reportDocuments);
        }
        return ResponseEntity.badRequest().build();
    }

and the according MongoRepository

package com.my.personal.app.repository;

import com.my.personal.app.document.ReportDocument;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface MongoReportRepository extends MongoRepository<ReportDocument, String> {

    List<ReportDocument> findByVehicleGuidOrderByStartAsc(String vehicleGuid);

    List<ReportDocument> findByUserGuidOrderByStartAsc(String userGuid);

    List<ReportDocument> findByUserGuidAndVehicleGuidOrderByStartAsc(String userGuid, String vehicleGuid);

}
Bugra
  • 189
  • 2
  • 14
  • 1
    write your custom converter and register into MongoConfiguration – Viet Apr 21 '17 at 15:02
  • You can follow the steps here to create custom conversion here. http://stackoverflow.com/questions/41127665/zoneddatetime-with-mongodb/. Also read https://jira.spring.io/browse/DATACMNS-698 – s7vr Apr 21 '17 at 15:06
  • @Veraam : I adapted the converters and registered them. But the application is not affected and the same exception occurs. Is there sth. that I missed? – Bugra Apr 22 '17 at 10:02
  • Can I see a sample document from your collection along with calling code ? How are the fields saved in database ? – s7vr Apr 22 '17 at 10:04
  • Is your configuration picked up at all ? – s7vr Apr 22 '17 at 10:20
  • I updated my post with an excerpt from my documents and the REST controller's calling code. What do you mean when saying whether the configuration is picked up at all? What can I do to check your mentioned point? – Bugra Apr 22 '17 at 10:25
  • Sorry I was just asking if your configuration is working at all but I think I see the problem. You have to save the fields as Date type not OffsetDateTime fields. Spring converters will convert them from date to OffsetDateTime and vice versa. – s7vr Apr 22 '17 at 10:29
  • Ah, I see. Thank you for the hint. But that also means, that I can not make use of any offset data or even zoned date data. The meta information will be lost and I can make use only of Date or LocalDateTime. Thanks for the important hint. Otherwise I would have searched endlessly and having a beard now. I will set your answer as the helpful one. Thanks a lot. – Bugra Apr 22 '17 at 10:32

1 Answers1

4

I stuggled with this for a fair few hours. The error I was getting was:

No converter found capable of converting from type [java.util.Date] to type [java.time.OffsetDateTime]

I eventually came up with the following config class that converts to a java.util.Date rather than a String:

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Date;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;

@Configuration
public class MongoConfig {

    @Bean
    public MongoCustomConversions mongoCustomConversions() {
        return new MongoCustomConversions(Arrays.asList(
            new OffsetDateTimeReadConverter(),
            new OffsetDateTimeWriteConverter()
        ));
    }

    static class OffsetDateTimeWriteConverter implements Converter<OffsetDateTime, Date> {

        @Override
        public Date convert(OffsetDateTime source) {
            return source == null ? null : Date.from(source.toInstant().atZone(ZoneOffset.UTC).toInstant());
        }
    }

    static class OffsetDateTimeReadConverter implements Converter<Date, OffsetDateTime> {

        @Override
        public OffsetDateTime convert(Date source) {
            return source == null ? null : source.toInstant().atOffset(ZoneOffset.UTC);
        }
    }
}

This worked for me when building against:

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/>
  </parent>
theINtoy
  • 3,388
  • 2
  • 37
  • 60
  • 1
    I suspect, the reason this conversion doesn't exist by default, is because conversion from OffsetDateTime to Date is lossy. If you have 12:00:00-07:00 converted to Date, what you store in DB is just 05:00:00Z. When you grab it back from db, you don't know the original offset. You can create a new OffsetDateTime, but it won't have the original offset. To store it fully you would need at least two columns/fields (the instant + the offset). And this is something that has to be done consciously. – iwat0qs Nov 12 '21 at 14:09