【问题标题】:Spring Boot webflux with reactive mongodb. java.lang.NoSuchMethodException: com.acme.dao.model.Transit.<init>()带有反应式 mongodb 的 Spring Boot webflux。 java.lang.NoSuchMethodException: com.acme.dao.model.Transit.<init>()
【发布时间】:2021-09-05 22:15:36
【问题描述】:

我无法将我的存储库连接到我的本地 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 问题来看,我认为这可能是驱动程序兼容性问题。我已经运行了 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 版,所以我不知道该怎么做。或者是否可能有其他问题?

【问题讨论】:

  • 它说Transit using constructor NO_CONSTRUCTOR with arguments,但你已经把@NoArgsConstructor。您是否进行了清洁和安装?
  • 在您编写的 Json 文件中 _id ,但在您的模型中是 id 。所以把String id改成String _id再试一次。
  • @varman 尝试运行 clean 并再次构建(我没有安装为 gradle 脚本)和同样的问题。
  • @mr1554 感谢您的建议,我尝试使用_id 并尝试将字符串更改为ObjectId,这是它使用的bson 类型(_idid)它仍然给构造函数错误。

标签: java spring mongodb spring-boot


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-02-08
    • 2020-04-05
    • 2018-06-24
    • 2019-04-12
    • 2019-01-05
    • 2021-09-08
    • 2020-10-24
    • 2021-11-18
    • 2020-04-29
    相关资源
    最近更新 更多