i'm facing this error : Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'
with my spring boot app not knowing the reason or what to do , i'm using both mySql and mongoDb in one app which i already did before and its working the same way in an other app with no problem , i don't know exactly what to do with it or why this error is showing ,generally this is my code :
app props :
spring.application.name=Inventory
spring.datasource.url=jdbc:mysql://localhost:3306/inventory
spring.datasource.username=root
spring.datasource.password=
spring.data.mongodb.uri=mongodb://localhost:27017/
spring.data.mongodb.database=mongoInventory
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
logging.level.org.springframework.data.mongodb=DEBUG
logging.level.org.springframework=DEBUG
server.error.include-exception=true
server.error.include-stacktrace=always
spring.main.allow-circular-references=true
logging.level.org.hibernate=TRACE
Gradle Build :
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.5'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'com.InventoryCHU'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '21'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
implementation group: 'org.python', name: 'jython-slim', version: '2.7.3'
compileOnly 'org.projectlombok:lombok'
implementation 'com.h2database:h2'
implementation 'io.jsonwebtoken:jjwt:0.9.1' // JWT library
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
all my reps are like that :
package com.InventoryCHU.Inventory.Repository;
import com.InventoryCHU.Inventory.Models.BreakDown;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.Optional;
public interface BreakDownRepository extends MongoRepository<BreakDown , String> {
Optional<BreakDown> findBreakDownByItemId(String item);
}
Service :
package com.InventoryCHU.Inventory.Services;
import com.InventoryCHU.Inventory.Models.BreakDown;
import com.InventoryCHU.Inventory.Repository.BreakDownRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class BreakDownService {
@Autowired
BreakDownRepository breakDownRepository;
public List<BreakDown> getAllBreakDowns(){
return breakDownRepository.findAll();
}
public Optional<BreakDown> getBreakDownById(String Id){
return breakDownRepository.findById(Id);
}
public BreakDown createBreakDown(BreakDown breakDown){
return breakDownRepository.save(breakDown);
}
public void deleteBreakDown(String Id){
breakDownRepository.deleteById(Id);
}
public Optional<BreakDown> findBreakDownByItemId(String productId){ return breakDownRepository.findBreakDownByItemId(productId); }
}
controller :
package com.InventoryCHU.Inventory.Controllers;
import com.InventoryCHU.Inventory.Models.BreakDown;
import com.InventoryCHU.Inventory.Services.BreakDownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/breakdown")
public class BreakDownController {
@Autowired
BreakDownService breakDownService;
@GetMapping
public ResponseEntity<List<BreakDown>> getAllBreakDowns() {
List<BreakDown> breakDowns = breakDownService.getAllBreakDowns();
return ResponseEntity.ok(breakDowns);
}
@GetMapping("/{id}")
public ResponseEntity<BreakDown> getBreakDownById(@PathVariable String id) {
Optional<BreakDown> breakDown = breakDownService.getBreakDownById(id);
return breakDown.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@GetMapping("/product-{id}")
public ResponseEntity<BreakDown> getBreakDownByUserId(@PathVariable("id") String productId){
Optional<BreakDown> breakDown = breakDownService.findBreakDownByItemId(productId);
return breakDown.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@PostMapping(consumes = "application/json", produces = "application/json")
public ResponseEntity<BreakDown> createBreakDown(@RequestBody BreakDown breakDown) {
BreakDown createdBreakDown = breakDownService.createBreakDown(breakDown);
return ResponseEntity.status(HttpStatus.CREATED).body(createdBreakDown);
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<Void> deleteBreakDown(@PathVariable String id) {
breakDownService.deleteBreakDown(id);
return ResponseEntity.noContent().build();
}
}
for the error showing its too long so i think its better to only show this :
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'breakDownController': Unsatisfied dependency expressed through field 'breakDownService': Error creating bean with name 'breakDownService': Unsatisfied dependency expressed through field 'breakDownRepository': Error creating bean with name 'breakDownRepository' defined in com.InventoryCHU.Inventory.Repository.BreakDownRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'breakDownService': Unsatisfied dependency expressed through field 'breakDownRepository': Error creating bean with name 'breakDownRepository' defined in com.InventoryCHU.Inventory.Repository.BreakDownRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'breakDownRepository' defined in com.InventoryCHU.Inventory.Repository.BreakDownRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mongoTemplate' parameter 1: Error creating bean with name 'mappingMongoConverter' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mappingMongoConverter' parameter 1: Error creating bean with name 'mongoMappingContext' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.class]: Unable to make field private java.util.Date java.security.Timestamp.timestamp accessible: module java.base does not "opens java.security" to unnamed module @3c9754d8
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mappingMongoConverter' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mappingMongoConverter' parameter 1: Error creating bean with name 'mongoMappingContext' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.class]: Unable to make field private java.util.Date java.security.Timestamp.timestamp accessible: module java.base does not "opens java.security" to unnamed module @3c9754d8
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoMappingContext' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.class]: Unable to make field private java.util.Date java.security.Timestamp.timestamp accessible: module java.base does not "opens java.security" to unnamed module @3c9754d8
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private java.util.Date java.security.Timestamp.timestamp accessible: module java.base does not "opens java.security" to unnamed module @3c9754d8``
i don't know exactly what to try , i looked on google but i didn't find anything about it , i also tried gpt all it said is incompatible version between java and mongodb and UnsatisfiedDependencyException related to the mongoTemplate bean without giving any working solution , if any idea for the solution i will be glad for it
after testing for one day the whole error was related to using timeStamp type instead of LocalDateTime , in case you passed by this error and you know a way how to manage displaying errors in a more detailed way in spring boot other than :
server.error.include-exception=true
server.error.include-stacktrace=always
please mention it on a comment.