Quantcast
Channel: Active questions tagged mongodb-atlas - Stack Overflow
Viewing all articles
Browse latest Browse all 279

SpringBoot + MongoDB "Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'"

$
0
0

I am new to Spring Boot, following tutorials to create a simple restAPI using MongoDB, and I keep encountering this same error " Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' My db is hosted on atlas.

GroceryItem

package com.github.nohgo.grocerylist.models;import lombok.Builder;import lombok.Data;import org.springframework.data.mongodb.core.mapping.Document;import org.springframework.data.annotation.Id;@Data@Builder@Document(collection = "grocery-item")public class GroceryItem {    @Id    private String id;    private String name;    private int quantity;    private String category;    @Override    public String toString() {        return "GroceryItem [id=" + id +", name=" + name +", quantity=" + quantity +", category=" + category +"]";    }}

ItemRepository

package com.github.nohgo.grocerylist.repository;import com.github.nohgo.grocerylist.models.GroceryItem;import org.springframework.data.mongodb.repository.MongoRepository;import org.springframework.stereotype.Repository;import java.util.List;@Repositorypublic interface ItemRepository extends MongoRepository<GroceryItem, String> {}

ItemService

package com.github.nohgo.grocerylist.service;import com.github.nohgo.grocerylist.models.GroceryItem;public interface ItemService {    String save (GroceryItem item);}

ItemServiceImpl

package com.github.nohgo.grocerylist.service;import com.github.nohgo.grocerylist.models.GroceryItem;import com.github.nohgo.grocerylist.repository.ItemRepository;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class ItemServiceImpl implements ItemService {    @Autowired    private ItemRepository itemRepository;    @Override    public String save(GroceryItem item) {        return itemRepository.save(item).getId();    }}

ItemController

package com.github.nohgo.grocerylist.controller;import com.github.nohgo.grocerylist.models.GroceryItem;import com.github.nohgo.grocerylist.service.ItemService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api/item")public class ItemController {    @Autowired    private ItemService itemService;    @PostMapping    public String save(@RequestBody GroceryItem item){        return itemService.save(item);    }}

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>3.2.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.github.nohgo</groupId><artifactId>GroceryList</artifactId><version>0.0.1-SNAPSHOT</version><name>GroceryList</name><description>GroceryList</description><properties><java.version>17</java.version></properties><dependencies><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><dependency><groupId>org.mongodb</groupId><artifactId>mongodb-driver-sync</artifactId><version>5.0.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>annotationProcessor</scope></dependency><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.3.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

application.properties

spring.application.name=GroceryListspring.data.mongodb.uri=mongodb+srv://username:password@grocery-list.6iphtjn.mongodb.net/spring.data.mongodb.database=mygrocerylistspring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER

From here I expected to be able to launch my VM and start sending a post request to my api, but instead I am getting this error on startup (only posted a line because block was marked as spam)

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemController': Unsatisfied dependency expressed through field 'itemService': Error creating bean with name 'itemServiceImpl': Unsatisfied dependency expressed through field 'itemRepository': Error creating bean with name 'itemRepository' defined in com.github.nohgo.grocerylist.repository.ItemRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'

I know this error is likely just me making some stupid mistake but I am unable to see where I deviate from these tutorials. Any help is appreciated.

Edit:

GroceryListApplication

package com.github.nohgo.grocerylist;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class GroceryListApplication {    public static void main(String[] args) {        SpringApplication.run(GroceryListApplication.class, args);    }}

Viewing all articles
Browse latest Browse all 279

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>