首页 > 解决方案 > 带有反应式 mongodb 的 Spring Boot webflux。java.lang.NoSuchMethodException:com.acme.dao.model.Transit。()

问题描述

我无法将我的存储库连接到我的本地 mongo db 实例。我认为一切都设置正确,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 问题来看,我认为这可能是驱动程序兼容性问题。我已经5.0.2运行了 mongodb 版本。这是我的 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 版,所以我在不知所措。或者是否有可能还有其他问题?

标签: javaspringmongodbspring-boot

解决方案


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


推荐阅读