I have created an annotated @RestController
TopicController
and it has the field called TopicService
@Autowired
annotated. My only method getAllTopicss()
must return List of Topic, which I get from declaring field topicService
, but Spring can not run the Application (It can not find proper bean). If you ever met this problem, please share your experience (I will print the Error text and the code below this description)
Topic Service class:
@Service
public class TopicService {
private List<Topic> topics = Arrays.asList(
new Topic(),
new Topic(),
new Topic()
);
public List<Topic> getAllTopics() {
return topics;
}
}
Topic Controller class:
@RestController
public class TopicController {
@Autowired
private TopicService topicService;
@RequestMapping("/topics")
public List<Topic> getAllTopicss() {
return topicService.getAllTopics();
}
}
And the Spring Exception:
APPLICATION FAILED TO START
Description:
Field topicService in io.java.springBoot.TopicController required a bean of type 'io.java.Topic.TopicService' 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 'io.java.Topic.TopicService' in your configuration.