0

I'm struggling through a Spring Web API tutorial, using STS4.

I got to the state in the tutorial where I need to test it, but I just got a "not found", so the routing doesn't seem to be working.

I saw online comments that seem to suggest I need a @ComponentScan("com.example") adding to the main() function, something not mentioned in the tutorial.

So I did this but now I'm getting the following.

***************************
APPLICATION FAILED TO START
***************************

Description:

Field pollRepository in com.example.controller.PollController required a bean of type 'com.example.repository.PollRepository' that could not be found.

The injection point has the following annotations:
    - @javax.inject.Inject()


Action:

Consider defining a bean of type 'com.example.repository.PollRepository' in your configuration.

My project looks like this:

enter image description here

The main method (in QuickPollApplication.java):

package com.example.rest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.example")
@SpringBootApplication
public class QuickPollApplication {

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

}

and PollController.java looks like this:

package com.example.controller;

import javax.inject.Inject;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.Poll;
import com.example.repository.PollRepository;

@RestController
public class PollController {

    @Inject
    private PollRepository pollRepository;
    
    @GetMapping("/polls")
    public ResponseEntity<Iterable<Poll>> getAllPolls() {
        Iterable<Poll> allPolls = pollRepository.findAll();
        return new ResponseEntity<>(allPolls, HttpStatus.OK);
    }
}

and this is PollRepository.java:

package com.example.repository;

import org.springframework.data.repository.CrudRepository;
import com.example.domain.Poll;


public interface PollRepository extends CrudRepository<Poll, Long> {

}

Let me know if it would be helpful to show anything else!

As per a suggestion I tried adding @EnableJpaRepositories(basePackages="com.example") to the QuickPollApplication class, but this just resulted in more errors, starting with:

Error creating bean with name 'pollController': Unsatisfied dependency expressed through field 'pollRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pollRepository' defined in com.example.repository.PollRepository defined in @EnableJpaRepositories declared on QuickPollApplication: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.domain.Poll

2
  • 1
    I'm coming from the Spring Boot Context so I'm not sure, but you would need to anotate PollRepository with @Repository there. Is there an pendant for that in Spring?
    – Nico
    Oct 4, 2022 at 17:48
  • Didn't fix the issue, but good to know, thanks! Oct 4, 2022 at 18:47

1 Answer 1

1

Can you move application main class to com.example package instead?

If not, You can try adding
@EnableJpaRepositories(basePackages="com.example") to QuickPollApplication class.

Also you require additional config like @EntityScan with base packages configured.

Explaination:

By default, Spring Boot enables JPA repository support and looks in the package (and its subpackages) where @SpringBootApplication is located. If your configuration has JPA repository interface definitions located in a package that is not visible, you can point out alternate packages by using @EnableJpaRepositories and its type-safe basePackageClasses=MyRepository.class parameter.

Ref: https://spring.io/guides/gs/accessing-data-jpa/

5
  • Thanks. I tried that but got more errors, starting with Error creating bean with name 'pollController' (added to question). Oct 4, 2022 at 18:56
  • 1
    keep component scan as is do not remove it Oct 4, 2022 at 18:59
  • Yes I didn't remove that. Oct 4, 2022 at 19:00
  • 1
    Can you try moving QuickPollApplication to com.example package? Also ensure Poll class is Entity Oct 4, 2022 at 19:04
  • That worked! Awesome. Thanks so much :) If you update your answer I'll accept it Oct 4, 2022 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.