0

I am a new beginner in Spring boot...I encounter a problem When I run my controller,

Description:

Field todoService in com.springboot.todoController.TodoController required a bean of type 'com.springboot.todo.TodoService' that could not be found.

Action:

Consider defining a bean of type 'com.springboot.todo.TodoService' in your configuration.

below is my code

Todo.java

package com.springboot.todoBean;

import java.util.Date;

public class Todo {
    private int id;
    private String user;

    private String desc;

    private Date targetDate;
    private boolean isDone;

    public Todo() {}

    public Todo(int id, String user, String desc, Date targetDate, boolean isDone) {
        super();
        this.id = id;
        this.user = user;
        this.desc = desc;
        this.targetDate = targetDate;
        this.isDone = isDone;
    }


    public int getId() {
        return id;
    }


    public String getUser() {
        return user;
    }


    public String getDesc() {
        return desc;
    }


    public Date getTargetDate() {
        return targetDate;
    }


    public boolean isDone() {
        return isDone;
    }

}

TodoService.java

package com.springboot.todo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.springboot.todoBean.Todo;

@Service
public class TodoService {
    private static List<Todo> todos = new ArrayList<Todo>();
    private static int todoCount = 3;


    static {
        todos.add(new Todo(1, "Jack", "Learn Spring MVC", new Date(), false));
        todos.add(new Todo(2, "Jack", "Learn Struts", new Date(), false));
        todos.add(new Todo(3, "Jill", "Learn hibernate", new Date(), false));
    }

    public List<Todo> retrieveTodos(String user){
        List<Todo> filteredTodos = new ArrayList<Todo>();
        for (Todo todo : todos) {
            if(todo.getUser().equals(user))
                filteredTodos.add(todo);
        }
        return filteredTodos;
    }

    public Todo addTodo(String name, String desc,
            Date targetDate, boolean isDone) {
        Todo todo = new Todo(++todoCount, name, desc, targetDate, isDone);
        todos.add(todo);
        return todo;
    }

    public Todo retrievedTodo(int id) {
        for(Todo todo: todos) {
            if(todo.getId() == id)
                return todo;    
        }
        return null;
    }
}

TodoController.java

package com.springboot.todoController;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.todo.TodoService;
import com.springboot.todoBean.Todo;

@RestController
public class TodoController {

    @Autowired
    private TodoService todoService;

    @GetMapping("/users/{name}/todos")
    public List<Todo> retrieveTodo(@PathVariable String name){
        return todoService.retrieveTodos(name);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(TodoController.class, args);
    }
}

enter image description here I have added the annotation @Service to the TodoService to tell spring boot it is a bean, but it still cannot recognise, could anyone tell me how to solve this problem? thanks

HungryBird
  • 1,077
  • 1
  • 11
  • 28
  • What package is you main class in? Post it fully. In your source layout it should be “com.springboot” package. – Strelok Oct 08 '18 at 10:45
  • Where is your class annotated with @SpringBootApplication? It should be among parent packages of com.springboot.todo . – Ilya Sazonov Oct 08 '18 at 10:45

2 Answers2

3

Create a separate class as below for start the spring boot application. Also, please note that below class should be placed in higher level in the package hierarchy than other controller, service, etc classes.

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Janath
  • 137
  • 9
1

Your error is generated because your application is not scanning TodoService. The mentioned code has several problems:

  1. please make all packages lower case - java convention
  2. please move the main in another class and annotate it with @SpringBootApplication

ex.

@SpringBootApplication
public class Application{

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

3. by default the spring boot application will scan beans contained in the package where the class from 2 is defined. You can put the class from 2 in the common package for both service and controller.

Adina Rolea
  • 2,031
  • 1
  • 7
  • 16