Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot NoSuchMethodError

I am building a simple url shortener connected to MongoDB Atlas with a JPA repository, and when I try to save the url data when the request hits the post request, I get the following error: java.lang.NoSuchMethodError: com.mongodb.client.MongoCollection.insertOne(Ljava/lang/Object;)Lcom/mongodb/client/result/InsertOneResult;. According the research, I believe it is a dependency issue but was not able to resolve it.

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:[email protected]/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>
 

Any help would be greatly appreciated!

like image 602
louprogramming Avatar asked Jul 18 '20 23:07

louprogramming


Video Answer


2 Answers

The issue is you are providing boot starter and mongo driver like below.

<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>

whereas spring-boot-starter-data-MongoDB has a java driver dependency of 4.0.4. So 2 different versions are colliding. just remove your explicit mongo driver dependency.

<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>
like image 189
Amit Vyas Avatar answered Oct 24 '22 08:10

Amit Vyas


One of the features of spring-boot-starter-parent is that it manages versions of many common dependencies for you, ensuring that the versions of all the different pieces you're using are compatible. In this case, your explicit version is causing incompatibility between Spring Data MongoDB and the MongoDB driver; simply eliminate the version tags from your dependencies.

(You may be getting a warning about "overriding managed dependency version"; always pay attention to warnings.)

like image 28
chrylis -cautiouslyoptimistic- Avatar answered Oct 24 '22 10:10

chrylis -cautiouslyoptimistic-