No bean named ‘mongoTemplate’ available. Spring Boot + MongoDB.

huangapple go评论79阅读模式
英文:

No bean named 'mongoTemplate' available. Spring Boot + MongoDB

问题

I'm building a backend using Spring Boot and MongoDB, first I'm making the user repository, service, and controller, and getting this error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userRepository in com.package.package.controller.UserController required a bean named 'mongoTemplate' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean named 'mongoTemplate' in your configuration.

Here's the code:

UserController:

package com.package.package.controller;

import com.package.package.entities.User;
import com.package.package.repositories.UserRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping(value = "/")
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    @GetMapping(value = "/{id}")
    public User getUserById(@PathVariable("id") ObjectId id) {
        return userRepository.findBy_id(id);
    }

    @PutMapping(value = "/{id}")
    public void modifyUserById(@PathVariable("id") ObjectId id, @Valid @RequestBody User user) {
        user.set_id(id);
        userRepository.save(user);
    }

    @PostMapping(value = "/")
    public User createUser(@Valid @RequestBody User user) {
        user.set_id(ObjectId.get());
        userRepository.save(user);
        return user;
    }

    @DeleteMapping(value = "/{id}")
    public void deleteUser(@PathVariable ObjectId id) {
        userRepository.delete(userRepository.findBy_id(id));
    }
}

UserRepository:

package com.package.package.repositories;

import com.package.package.entities.User;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends MongoRepository<User, String> {
    User findBy_id(ObjectId _id);
}

UserService:

package com.package.package.service;

import com.package.package.entities.User;
import org.bson.types.ObjectId;

import java.util.List;

public interface UserService {
    User save(User user);
    User findbyid(ObjectId _id);
    List<User> getAll();
    void delete(User user);
}

MainApplication:

package com.package.package;

import com.package.package.repositories.UserRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = UserRepository.class)
public class TestApplication {

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

}

I am using as a reference a past project that I made (the difference is that this was done with postgres). Any other information you want, just tell me.

UPDATE:
Here's the 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.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.wazzka</groupId>
	<artifactId>wazzka</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>wazzka</name>
	<description>WazzkaProject</description>

	<properties>
		<java.version>14</java.version>
		<spring.framework.version>5.2.8.RELEASE</spring.framework.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</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.springframework.data</groupId>
			<artifactId>spring-data-mongodb</artifactId>
			<version>3.0.3.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-releasetrain</artifactId>
			<version>Lovelace-SR9</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-commons</artifactId>
			<version>2.2.3.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>jakarta.validation</groupId>
			<artifactId>jakarta.validation-api</artifactId>
			<version>2.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
			<version>2.2.3.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>
	</dependencies>

	<repositories>
		<repository>
			<id>spring-milestone</id>
			<name>Spring Maven MILESTONE Repository</name>
			<url>https://repo.spring.io/libs-milestone</url>
		</repository>
	</repositories>

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

</project>
英文:

I'm building a backend using Spring Boot and MongoDB, first I'm making the user repository, service, and controller, and getting this error:

***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.package.package.controller.UserController required a bean named &#39;mongoTemplate&#39; that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean named &#39;mongoTemplate&#39; in your configuration.

Here's the code:

UserController:

package com.package.package.controller;
import com.package.package.entities.User;
import com.package.package.repositories.UserRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping(&quot;/users&quot;)
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping(value = &quot;/&quot;)
public List&lt;User&gt; getAllUsers() {
return userRepository.findAll();
}
@GetMapping(value = &quot;/{id}&quot;)
public User getUserById(@PathVariable(&quot;id&quot;) ObjectId id) {
return userRepository.findBy_id(id);
}
@PutMapping(value = &quot;/{id}&quot;)
public void modifyUserById(@PathVariable(&quot;id&quot;) ObjectId id, @Valid @RequestBody User user) {
user.set_id(id);
userRepository.save(user);
}
@PostMapping(value = &quot;/&quot;)
public User createUser(@Valid @RequestBody User user) {
user.set_id(ObjectId.get());
userRepository.save(user);
return user;
}
@DeleteMapping(value = &quot;/{id}&quot;)
public void deleteUser(@PathVariable ObjectId id) {
userRepository.delete(userRepository.findBy_id(id));
}
}

UserRepository:

package com.package.package.repositories;
import com.package.package.entities.User;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends MongoRepository&lt;User, String&gt; {
User findBy_id(ObjectId _id);
}

UserService:

package com.package.package.service;
import com.package.package.entities.User;
import org.bson.types.ObjectId;
import java.util.List;
public interface UserService {
User save(User user);
User findbyid(ObjectId _id);
List&lt;User&gt; getAll();
void delete(User user);
}

MainApplication:

package com.package.package;
import com.package.package.repositories.UserRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = UserRepository.class)
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}

I am using as a reference a past project that I made (the difference is that this was done with postgres).
Any other information you want, just tell me.
Thanks in advance.

UPDATE:
Here's the pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.2.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.wazzka&lt;/groupId&gt;
&lt;artifactId&gt;wazzka&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;wazzka&lt;/name&gt;
&lt;description&gt;WazzkaProject&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;14&lt;/java.version&gt;
&lt;spring.framework.version&gt;5.2.8.RELEASE&lt;/spring.framework.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
&lt;artifactId&gt;spring-data-mongodb&lt;/artifactId&gt;
&lt;version&gt;3.0.3.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
&lt;artifactId&gt;spring-data-releasetrain&lt;/artifactId&gt;
&lt;version&gt;Lovelace-SR9&lt;/version&gt;
&lt;type&gt;pom&lt;/type&gt;
&lt;scope&gt;import&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
&lt;artifactId&gt;spring-data-commons&lt;/artifactId&gt;
&lt;version&gt;2.2.3.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;jakarta.validation&lt;/groupId&gt;
&lt;artifactId&gt;jakarta.validation-api&lt;/artifactId&gt;
&lt;version&gt;2.0.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
&lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
&lt;version&gt;2.2.3.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;repositories&gt;
&lt;repository&gt;
&lt;id&gt;spring-milestone&lt;/id&gt;
&lt;name&gt;Spring Maven MILESTONE Repository&lt;/name&gt;
&lt;url&gt;https://repo.spring.io/libs-milestone&lt;/url&gt;
&lt;/repository&gt;
&lt;/repositories&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

答案1

得分: 33

尝试添加此依赖项:

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

Try adding this dependency:

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
&lt;/dependency&gt;

答案2

得分: 5

在这种情况下,这绝对不是大多数情况下错误的原因,但它确实发生在我身上,我花了相当多的时间寻找原因,所以我想在这里发布“解决方案”。

在我的情况下,在最初禁用自动配置(application.yml)后,我忘记删除以下这些行:

spring.autoconfigure.exclude:
  - org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
  - org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
英文:

While this is certainly not the reason for the error in most cases, the fact that it happened to me and I spent a fair amount of time looking for the reason made me want to post "the solution" here.

In my case I forgot to remove these lines again after disabling autoconfiguration initially (application.yml):

spring.autoconfigure.exclude:
  - org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
  - org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration

答案3

得分: 2

希望这个答案也能帮助到某人。

如果您在代码块中具有以下带有参数的注释,请将其删除。

exclude = {MongoAutoConfiguration.class} 

例如:

@SpringBootApplication(exclude = {MongoAutoConfiguration.class})

之后:

@SpringBootApplication
英文:

Hopefully, this answer too can help someone.

If you have following annotation with the parameter somewhere in your code block remove it.

exclude = {MongoAutoConfiguration.class} 

Ex: Before

@SpringBootApplication(exclude = {MongoAutoConfiguration.class})

After

@SpringBootApplication

答案4

得分: 1

我之前的配置是:

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

我只是移除了版本标签,然后它就起作用了。
英文:

I Had

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
&lt;version&gt;1.5.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;

Just removed the version tag and its worked

huangapple
  • 本文由 发表于 2020年8月13日 07:44:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63386079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定