Spring Boot webflux with reactive mongodb.java.lang.NoSuchMethodException: com.acme.dao.model.Transi



我在连接存储库到mongodb的本地实例时遇到了问题。我认为一切都设置正确,mongo compass文件的形状看起来和POJO一样。但是当我运行它时,我得到了这个错误

2021-09-05 16:08:37.745 ERROR 19564 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate com.acme.dao.model.Transit using constructor NO_CONSTRUCTOR with arguments ] with root cause

从其他SO问题,我认为这可能是一个驱动程序兼容性问题。我有版本的mongodb运行5.0.2。这是我的gradle文件

plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.foretold'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive:2.5.3'
implementation 'org.springframework.boot:spring-boot-starter-data-rest:2.5.4'
implementation 'org.springframework.boot:spring-boot-starter-webflux:2.5.4'
implementation 'org.projectlombok:lombok:1.18.20'
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.4'
testImplementation 'de.flapdoodle.embed:de.flapdoodle.embed.mongo:3.0.0'
testImplementation 'io.projectreactor:reactor-test:3.4.9'
}
test {
useJUnitPlatform()
}

这是POJO

@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Transit {
@Id
private String id;
private String planetName;
private Double decimal;
private Long epochSeconds;
private String key;
private Double speed;
public Transit(String planetName) {
this.planetName = planetName;
}
public Transit(String planetName, String key) {
this.planetName = planetName;
this.key = key;
}
}

这是一个示例文档,因为它现在在我的收藏中。

{"_id":{"$oid":"613520c592d3c015a17c7029"},
"planetName":"Pluto",
"decimal":75.27915837864154,
"key":"075-16",
"speed":0.01849046574763826,
"epochSeconds": {"$numberLong":"-2198981873"}}

这里的文档https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#compatibility.matrix似乎根本没有引用mongo的版本5,所以我不知道该怎么做。或者是否有可能是其他地方出了问题?

问题出在lombok注释上。当我删除它们并手动添加所有构造函数时,它工作了。然后我遇到了一个问题,它实际上在查询时为对象设置数据,这是通过用手动添加的getter和setter替换@Data注释来修复的。

相关内容

最新更新