JavaEar 专注于收集分享传播有价值的技术资料

'Field required a bean of type that could not be found.' error spring restful API using mongodb

So I've been learning Spring in the couples of week, been following this tutorial

Building a RESTful Web Service

All was well until I tried to integrate it to mongodb. So I follow this tutorial.

Accessing Data with MongoDB

But my practice is partially still using the first one. So my project directory structure is like this.

src/
├── main/
│   └── java/
|       ├── model/
|       |   └── User.java
|       ├── rest/
|       |   ├── Application.java
|       |   ├── IndexController.java
|       |   └── UsersController.java
|       └── service/
|           └── UserService.java
└── resources/
    └── application.properties

This is my model/User.java file

package main.java.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection="user")
public class User {

    private int age;
    private String country; 
    @Id
    private String id;
    private String name;


    public User() {
        super();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

This is my rest/UsersController.java file

package main.java.rest;

import java.util.List;
import main.java.service.UserService;
import main.java.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/users")
public class UsersController {

    @Autowired
    UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public List<User> getAllUsers() {
        return userService.findAll();
    }
}

This is my service/UserService.java file

package main.java.service;

import java.util.List;
import main.java.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface UserService extends MongoRepository<User, String> {
    public List<User> findAll();
}

I could compile them (I'm using gradle for compilation because I'm following the tutorial), but when I run the jar file it was throwing this error.


APPLICATION FAILED TO START


Description:

Field userService in main.java.rest.UsersController required a bean of type 'main.java.service.UserService' that could not be found.

Action:

Consider defining a bean of type 'main.java.service.UserService' in your configuration.

Not sure what is wrong I start googling around and found that I need to include Beans.xml file and register the userService in it. I did that but it's not working. I'm really new to this so I really have no clue on what's going on.

7个回答

    最佳答案
  1. Solved it. So by default, all packages that falls under @SpringBootApplication declaration will be scanned.

    Assuming my main class ExampleApplication that has @SpringBootApplication declaration is declared inside com.example.something, then all components that falls under com.example.something is scanned while com.example.applicant will not be scanned.

    So, there are two ways to do it based on this question. Use

    @SpringBootApplication(scanBasePackages={
    "com.example.something", "com.example.application"})
    

    That way, the application will scan all the specified components, but I think what if the scale were getting bigger ?

    So I use the second approach, by restructuring my packages and it worked ! Now my packages structure became like this.

    src/
    ├── main/
    │   └── java/
    |       ├── com.example/
    |       |   └── Application.java
    |       ├── com.example.model/
    |       |   └── User.java
    |       ├── com.example.controller/
    |       |   ├── IndexController.java
    |       |   └── UsersController.java
    |       └── com.example.service/
    |           └── UserService.java
    └── resources/
        └── application.properties
    
  2. 参考答案2
  3. Add the @Service in the service/UserService.java.

  4. 参考答案3
  5. I also had the same error:

    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    Field repository in com.kalsym.next.gen.campaign.controller.CampaignController required a bean of type 'com.kalsym.next.gen.campaign.data.CustomerRepository' that could not be found.
    
    
    Action:
    
    Consider defining a bean of type 'com.kalsym.next.gen.campaign.data.CustomerRepository' in your configuration.de here
    

    And my packages were constructed in the same way as mentioned in the accepted answer. I fixed my issue by adding EnableMongoRepositories annotation in the main class like this:

    @SpringBootApplication
    @EnableMongoRepositories(basePackageClasses = CustomerRepository.class)
    public class CampaignAPI {
    
        public static void main(String[] args) {
            SpringApplication.run(CampaignAPI.class, args);
        }
    }
    
  6. 参考答案4
  7. Add the @Component in your controller class. May this work

  8. 参考答案5
  9. Spent a lot of time because of the auto-import. Intellij Idea somewhy imported @Service from import org.jvnet.hk2.annotations.Service; instead of import org.springframework.stereotype.Service;!

  10. 参考答案6
  11. I have same Issue, fixed by Adding @EnableMongoRepositories("in.topthree.util")

    package in.topthree.core;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
    
    import in.topthree.util.Student;
    
    @SpringBootApplication
    @EnableMongoRepositories("in.topthree.util")
    public class Run implements CommandLineRunner {
    
        public static void main(String[] args) {
            SpringApplication.run(Run.class, args);
            System.out.println("Run");
        }
    
        @Autowired
        private Process pr;
    
        @Override
        public void run(String... args) throws Exception {
            pr.saveDB(new Student("Testing", "FB"));
            System.exit(0);
        }
    
    }
    

    And My Repository

     package in.topthree.util;
    
        import org.springframework.data.mongodb.repository.MongoRepository;
    
        public interface StudentMongo extends MongoRepository<Student, Integer> {
    
            public Student findByUrl(String url);
        }
    

    Now Its Working

  12. 参考答案7
  13. I encountered the same issue and all I had to do was to place the Application in a package one level higher than the service, dao and domain packages.