SpringBoot NoSuchMethodError

lou*_*ing 5 java spring jpa mongodb spring-boot

我建立连接到MongoDB的Atlas公司,具有JPA库简单的URL缩短器,当我尝试保存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;
    }

}
 
Run Code Online (Sandbox Code Playgroud)

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.
    }
 
}
Run Code Online (Sandbox Code Playgroud)

应用程序属性:

spring.data.mongodb.uri=mongodb+srv://myname:password@testcluster-scgty.mongodb.net/dbname?retryWrites=true&w=majority
spring.data.mongodb.database=dbname 
Run Code Online (Sandbox Code Playgroud)

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>
 
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

chr*_*ke- 5

的功能之一spring-boot-starter-parent它为您管理许多常见依赖项的版本,确保您正在使用的所有不同部分的版本都是兼容的。在这种情况下,您的显式版本导致 Spring Data MongoDB 和 MongoDB 驱动程序之间不兼容;version只需从依赖项中删除标签即可。

(您可能会收到有关“覆盖托管依赖项版本”的警告;请始终注意警告。)


Ami*_*yas 5

问题是您正在提供如下所示的引导启动程序和 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>
Run Code Online (Sandbox Code Playgroud)

而 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>
Run Code Online (Sandbox Code Playgroud)