我正在尝试使用MongoDB数据库设置Spring Boot应用程序。以下是我所拥有的依赖项的摘录(在Gradle表示中)。
QueryBuilders.boolQuery()
.mustNot(QueryBuilders.existsQuery("paymentRetCode"))
我的@Document带注释的Java类包含OffsetDateTime属性。
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")
当我调用一个检索这些文档的REST控制器时,它失败并出现异常
@Document(collection = "reports")
public class ReportDocument implements Serializable {
@Id private String id;
@Version private Long version;
//...
private OffsetDateTime start;
private OffsetDateTime end;
//...
}
我读了很多论坛。有些人用Joda-libraries DateTime替换了OffsetDateTime。这不是我的选择,因为Joda声明使用Java 8 DateTime-Types。
我做错了什么(我知道问题始终在电脑前),我该如何解决?有人对此有任何想法吗?
更新(2017年4月22日起) 我确实喜欢@Veeram说并用转换器更新了我的应用程序(Date - > OffsetDateTime,反之亦然)。
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]
和
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());
}
}
注册转换器
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());
}
}
但又导致异常
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);
}
}
我是否遗漏或误解了某事。或做某事错?
更新我的文档
以下是我的收藏文件的基本部分的摘录
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]
这是试图调用文档的REST控制器
[
{
"_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"
}
]
}
]
和相应的MongoRepository
@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();
}
答案 0 :(得分:0)
我为此奋斗了好几个小时。我得到的错误是:
No converter found capable of converting from type [java.util.Date] to type [java.time.OffsetDateTime]
我最终想到了以下配置类,该配置类转换为java.util.Date
而不是字符串:
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);
}
}
}
当针对以下内容进行构建时,这对我有用:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>