0

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.

tgallei
  • 827
  • 3
  • 13
  • 22
Mikhail
  • 195
  • 2
  • 12
  • 5
    You're not showing your Spring Configuration. Looks like Spring doesn't scan the package where TopicService lives... – Turo Jun 03 '20 at 07:47
  • What package hierarchy have you? See https://stackoverflow.com/questions/42907553/field-required-a-bean-of-type-that-could-not-be-found-error-spring-restful-ap maybe you have another package with is not scanned by spring – P3Ri Jun 03 '20 at 07:56
  • About configuration - of course, I have found the mistke. My XML was wrong. Thank you! – Mikhail Jun 04 '20 at 17:31

0 Answers0