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:
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
Advertisement
Answer
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.