Issue
The problem appeared when I tried to migrate on WebFlux.
I have one package university
. It contains 4 files: Document, Controller, Service and Repository.
@Document
data class University(
@Id
@JsonSerialize(using = ToStringSerializer::class)
var id: ObjectId?,
var name: String,
var city: String,
var yearOfFoundation: Int,
@JsonSerialize(using = ToStringSerializer::class)
var students: MutableList<ObjectId> = mutableListOf(),
@Version
var version: Int?
)
@Service
class UniversityService(@Autowired private var universityRepository: UniversityRepository) {
fun getAllUniversities(): Flux<University> =
universityRepository.findAll()
fun getUniversityById(id: ObjectId): Mono<University> =
universityRepository.findById(id)
}
@RestController
@RequestMapping("api/universities", consumes = [MediaType.APPLICATION_NDJSON_VALUE])
class UniversityController(@Autowired val universityService: UniversityService) {
@GetMapping("/all")
fun getAll(): Flux<University> =
universityService.getAllUniversities().log()
@GetMapping("/getById")
fun getUniversityById(@RequestParam("id") id: ObjectId): Mono<University> =
universityService.getUniversityById(id)
}
@Repository
interface UniversityRepository: ReactiveMongoRepository<University, ObjectId>, CustomUniversityRepository {
fun existsByNameIgnoreCase(name: String): Mono<Boolean>
fun removeUniversityById(id: ObjectId): Mono<University?>
fun findUniversitiesByNameIgnoreCase(name: String): Flux<University>
}
All in separate files regarding their names.
I am getting a problem with my service, cause it cannot find repository. Consider defining a bean of type 'demo.university.UniversityRepository' in your configuration.
But my repository file with exact name and interface is directly there.
I've tried to mark my repository with Bean
annotation, but I can't do so with interfaces. Also, @EnableJpaRepositories
does not help.
P.S. I know, it seems like a duplicate, but I really didn't find an answer in previous questions.
Solution
My problem was in a wrong project dependencies. As I mentioned, I migrated from simple Web to WebFlux. But I didn't change my MongoDB dependency. It should be marked as a reactive explicitly even if ReactiveMongoRepository
interface is found correctly.
implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive:2.7.1")
Answered By - Laughing_Man
Answer Checked By - Mildred Charles (JavaFixing Admin)