I am attempting to write a simple MongoDB app with the spring cli. I used this spring with mongodb tutorial as a base, and I am getting an an UnsatisfiedDependencyException error.

Here's my code

file: Grabs.groovy

@Grab('spring-boot-starter-data-mongodb')

class Grabs {}

file: CustomerOrder.groovy

import org.springframework.data.annotation.Id

class CustomerOrder {
    @Id
    String id
    Date orderDate
}

file: MongoOrderRepository.groovy

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

interface MongoOrderRepository extends MongoRepository<CustomerOrder, String> {
    CustomerOrder findById(String customerId)
}

file: OrderController.groovy

@RestController
class OrderController {

    @Autowired
    MongoOrderRepository orderRepository

    @GetMapping("/{order}")
    def listOrders(@PathVariable("order") String order) {
        List<CustomerOrder> orders = orderRepository.findById(order)
        return orders
    }
}

when I do a spring run *, here are the errors

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

Description:

Field orderRepository in OrderController required a bean of type 'MongoOrderRepository' 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 of type 'MongoOrderRepository' in your configuration.

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.cli.app.SpringApplicationLauncher.launch(SpringApplicationLauncher.java:68)
    at org.springframework.boot.cli.command.run.SpringApplicationRunner$RunThread.run(SpringApplicationRunner.java:168)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderController': Unsatisfied dependency expressed through field 'orderRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'MongoOrderRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    ... 6 more

I attempted to put in a @Repository in my MongoOrderRepository, since that worked for a Jdbc based implementation, but that did not work. I'm not sure how to proceed. Any help would be appreciated.

Thanks

1

There are 1 best solutions below

0
Thiago Chagas - Batata On BEST ANSWER

Running example: https://github.com/thiagochagas/groovy-mongodb

1) Adjusts in your code:

Grabs:

@Grab(group='org.springframework.boot', module='spring-boot-starter-data-mongodb', version='2.2.3.RELEASE')

MongoOrderRepository (Optional adjust):

interface MongoOrderRepository extends MongoRepository<CustomerOrder, String> {
    Optional<CustomerOrder> findById(String customerId)
}

OrderController (List to Optional):

@GetMapping("/{order}")
def listOrders(@PathVariable("order") String order) {
    Optional<CustomerOrder> orders = orderRepository.findById(order)
    return orders
}

2) Install and Running code:

./gradlew clean build
./gradlew bootRun

Accessing localhost:8080/{orderId} : enter image description here