title: 'Field required a bean of type that could not be found.' error spring restful API using mongodb
body: <p>So I've been learning Spring in the couples of week, been following this tutorial</p>
  
  <p><a href="https://spring.io/guides/gs/rest-service/" rel="noreferrer">Building a RESTful Web Service</a></p>
  
  <p>All was well until I tried to integrate it to mongodb. So I follow this tutorial.</p>
  
  <p><a href="https://spring.io/guides/gs/accessing-data-mongodb/" rel="noreferrer">Accessing Data with MongoDB</a></p>
  
  <p>But my practice is partially still using the first one. So my project directory structure is like this.</p>
  
  <pre><code>src/
  ├── main/
  │   └── java/
  |       ├── model/
  |       |   └── User.java
  |       ├── rest/
  |       |   ├── Application.java
  |       |   ├── IndexController.java
  |       |   └── UsersController.java
  |       └── service/
  |           └── UserService.java
  └── resources/
      └── application.properties
  </code></pre>
  
  <p>This is my <strong>model/User.java</strong> file</p>
  
  <pre class="lang-java prettyprint-override"><code>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;
      }
  }
  </code></pre>
  
  <p>This is my <strong>rest/UsersController.java</strong> file</p>
  
  <pre class="lang-java prettyprint-override"><code>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&lt;User&gt; getAllUsers() {
          return userService.findAll();
      }
  }
  </code></pre>
  
  <p>This is my <strong>service/UserService.java</strong> file</p>
  
  <pre class="lang-java prettyprint-override"><code>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&lt;User, String&gt; {
      public List&lt;User&gt; findAll();
  }
  </code></pre>
  
  <p>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.</p>
  
  <blockquote>
    <hr>
    
    <p>APPLICATION FAILED TO START</p>
    
    <hr>
    
    <p>Description:</p>
    
    <p>Field userService in main.java.rest.UsersController required a bean of
    type 'main.java.service.UserService' that could not be found.</p>
    
    <p>Action:</p>
    
    <p>Consider defining a bean of type 'main.java.service.UserService' in
    your configuration.</p>
  </blockquote>
  
  <p>Not sure what is wrong I start googling around and found that I need to include <code>Beans.xml</code> 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.</p>