gpt4 book ai didi

java - Spring Boot NoSuchMethodError

转载 作者:行者123 更新时间:2023-12-01 14:20:39 34 4
gpt4 key购买 nike

我正在构建一个连接到带有 JPA 存储库的 MongoDB Atlas 的简单 url 缩短器,当我尝试在请求命中 post 请求时保存 url 数据时,出现以下错误:java.lang.NoSuchMethodError: com.mongodb.client.MongoCollection.insertOne(Ljava/lang/Object;)Lcom/mongodb/client/result/InsertOneResult; .根据研究,我认为这是一个依赖性问题,但无法解决。
网址.java:

package com.sideproject.urlshortner.model;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Data
@Document(collection = "url")
public class URL {
@Id
private String id;
String longURL;
String shortenedURL;

public URL(String longURL, String shortenedURL) {
this.longURL = longURL;
this.shortenedURL = shortenedURL;
}

}

URLController.java:
package com.sideproject.urlshortner.controller;

import com.sideproject.urlshortner.repository.URLRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.sideproject.urlshortner.model.URL;


@RestController
@RequestMapping("/index")
public class URLController {

@Autowired
private URLRepository urlRepository;


@RequestMapping(value="/urls/", method=RequestMethod.POST)
public URL postURL(@RequestBody URL url) {
return urlRepository.save(url); // giving the error.
}

}
应用程序属性:
spring.data.mongodb.uri=mongodb+srv://myname:password@testcluster-scgty.mongodb.net/dbname?retryWrites=true&w=majority
spring.data.mongodb.database=dbname
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sideproject</groupId>
<artifactId>url-shortner</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>url-shortner</name>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.6</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

任何帮助将不胜感激!

最佳答案

问题是您正在提供如下所示的引导启动程序和 mongo 驱动程序。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.6</version>
</dependency>
而 spring-boot-starter-data-MongoDB 的 java 驱动程序依赖为 4.0.4。所以2个不同的版本发生冲突。只需删除您的显式 mongo 驱动程序依赖项。
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>2.3.1.RELEASE</springdata.commons>
<mongo>4.0.4</mongo>
<mongo.reactivestreams>${mongo}</mongo.reactivestreams>
<jmh.version>1.19</jmh.version>
</properties>

关于java - Spring Boot NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62974685/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com