SpringBoot NoSuchMethodError

时间:2020-07-18 23:20:11

标签: java spring mongodb spring-boot jpa

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

URL.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.
    }
 
}

application.properties:

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>
 

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:3)

问题是您正在提供如下所示的启动启动器和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具有4.0.4的Java驱动程序依赖性。因此有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>

答案 1 :(得分:2)

spring-boot-starter-parent的功能之一就是it manages versions of many common dependencies for you,确保您使用的所有不同作品的版本都兼容。在这种情况下,您的显式版本会导致Spring Data MongoDB与MongoDB驱动程序之间不兼容;只需从依赖项中消除version标签即可。

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