Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to MongoDB in springboot?

I am trying to create a basing spring app using mongoDB, but I don't know how to connect to the database. I tried something like this:

application.properties:

spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.database=mongulet
spring.datasource.driver-class-name=mongodb.jdbc.MongoDriver
spring.data.mongodb.port=27017

Main Application:

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RestSuperAdvancedApplication implements CommandLineRunner{

    @Autowired
    private CustomerRepository repository;

    public static void main(String[] args) {
        SpringApplication.run(RestSuperAdvancedApplication.class, args);
    }

    @Override
    public void run(String... strings) throws Exception {
        repository.deleteAll();

        repository.save(new Customer("Crisan", "Raoul"));
        repository.save(new Customer("Smith", "Martha"));
        repository.save(new Customer("Erie", "Jayne"));
        repository.save(new Customer("Robinson", "Crusoe"));

        System.out.println("Customers found : ");

        repository.findAll().forEach(System.out::println);

        System.out.println();

        System.out.println("Customer found by first name: (Erie)");
        System.out.println("----------------");
        System.out.println(repository.findOneByFirstName("Erie"));


    }
}

Customer class:

package com.example;

import javax.persistence.Id;

/**
 * Created by rcrisan on 7/19/2016.
 */
public class Customer {
    @Id
    private String id;
    private String firstName;
    private String lastName;

    public Customer() {
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id='" + id + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

Repository:

package com.example;

import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

/**
 * Created by rcrisan on 7/19/2016.
 */
public interface CustomerRepository extends MongoRepository<Customer, String> {
    Customer findOneByFirstName(String fistName);
    List<Customer> findByLastName(String lastName);
}

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>restsuperadvanced</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>restSuperAdvanced</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <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>
        </dependency>
    </dependencies>

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


</project>

After I run the program I get this exception:

Caused by: java.lang.IllegalStateException: Cannot load driver class: mongodb.jdbc.MongoDriver

Is there another way to connect to a mongoDB without using driver class ?

like image 955
Gustavo Avatar asked Dec 25 '22 03:12

Gustavo


1 Answers

You seem to be trying to mix JPA, which is primarily intended for relational datastores, with MongoDB, which is an "unrelated" document store. Drop the dependency on spring-boot-starter-data-jpa (you simply don't need it) and the spring.datasource.driver-class-name (you should use MongoDB natively, not via a JDBC bridge).

like image 72
chrylis -cautiouslyoptimistic- Avatar answered Dec 28 '22 08:12

chrylis -cautiouslyoptimistic-