0

I was following this guide to add MySql to an already existing SpringBoot project whose dependency management is on graddle. Just when I added these three class used in the tutorial as follow

main/java/net/code/model/Users.Java

package net.code.controller;
import net.code.model.User;
import net.code.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController   // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
    @Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;

    @GetMapping(path="/add") // Map ONLY GET Requests
    public @ResponseBody String addNewUser (@RequestParam String name
            , @RequestParam String email) {
        // @ResponseBody means the returned String is the response, not a view name
        // @RequestParam means it is a parameter from the GET or POST request

        User n = new User();
        n.setName(name);
        n.setEmail(email);
        userRepository.save(n);
        return "Saved";
    }

    @GetMapping(path="/all")
    public @ResponseBody Iterable<User> getAllUsers() {
        // This returns a JSON or XML with the users
        return userRepository.findAll();
    }
}

and a user repository as main/java/net/code/repo/UserRepository.Java package net.code.repo;

import net.code.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

}

with a webservice controller main/java/net/code/controller/MainController.Java

package net.code.controller;

import net.code.model.User;
import net.code.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController   // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
    @Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;

    @GetMapping(path="/add") // Map ONLY GET Requests
    public @ResponseBody String addNewUser (@RequestParam String name
            , @RequestParam String email) {
        // @ResponseBody means the returned String is the response, not a view name
        // @RequestParam means it is a parameter from the GET or POST request

        User n = new User();
        n.setName(name);
        n.setEmail(email);
        userRepository.save(n);
        return "Saved";
    }

    @GetMapping(path="/all")
    public @ResponseBody Iterable<User> getAllUsers() {
        // This returns a JSON or XML with the users
        return userRepository.findAll();
    }
}

My class with @SpringBoot main/java/net/code/App.Java

package net.code;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

//@CrossOrigin(origins = "http://127.0.0.1:8080")

@SpringBootApplication

@ComponentScan("net.code")
//@ComponentScan(basePackages = { "net.code","net.code.repo"})

//@RestController
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class App extends WebMvcConfigurerAdapter {

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

}

But any time I run the application, I keep getting the below message

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-10-21 15:11:59.674 ERROR 67424 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field userRepository in net.code.controller.MainController required a bean of type 'net.code.repo.UserRepository' that could not be found.


Action:

Consider defining a bean of type 'net.code.repo.UserRepository' in your configuration.


Process finished with exit code 1

I have search for relevant issue like these Spring Boot not autowiring @Repository, @RestController in other package doesn't work but couldn't fix as suggestion from those link didn't work for me I also wanted to try the accepted solution here Consider defining a bean of type 'package' in your configuration [Spring-Boot] but I find out that there is no such package for @EnableJpaRepositories

Please help me out on this as I have been trying to fix this for days now

olyjosh
  • 431
  • 7
  • 15
  • Try with `@EnableJpaRepositories(basePackages="")` – imk Oct 21 '17 at 15:09
  • You must be doing something wrong. This same code runs fine on my system – Olantobi Oct 21 '17 at 15:33
  • I know something is wrong, but is wrong ? However @EnableJpaRepositories doesn't compile in my IDE like a missing package that has this and which I don't know how to get – olyjosh Oct 21 '17 at 21:13
  • @olyjosh, Check the full source code here https://github.com/olantobi/spring-boot-repository – Olantobi Oct 22 '17 at 06:54
  • Be aware that SpringBootApplication is a heavily loaded annotation, intended to replace the others you also have above - https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html . This means that you're exclude on DataSourceAutoConfiguration for example may be ignored. You should consider bundling your exclude and your component scan into the @SpringBootApplication itself, see api docs for more - https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html – Aidan Moriarty Oct 22 '17 at 17:14
  • @Olantobi, thanks for the solution you posted on github, but I mentioned that my project dependency is on graddle and how do I add JPA data starter support in graddle, maybe with that I can use EnableJpaRepositories. – olyjosh Oct 23 '17 at 06:22
  • @olyjosh, you can check http://start.spring.io/ for a gradle version of jpa-starter – Olantobi Oct 23 '17 at 10:33
  • @Olantobi, you have really helped with your comment. The start.spring.io really helped me to clarify that I'm using the correct ID of the repository on graddle compile('org.springframework.boot:spring-boot-starter-data-jpa'). The DBConfig.java was also part of the solution. However EnableJpaRepositories wasn't used in my CrudeRepository class Ragards – olyjosh Oct 23 '17 at 13:21
  • I think @Olantobi should post the answer for accepted solution as your DBConfig.java give me a way to do this correctly – olyjosh Nov 03 '17 at 06:57

1 Answers1

0

A perfectly working version of this code is here on github. You can check http://start.spring.io to get the corresponding gradle version for spring-data-jpa.

Olantobi
  • 869
  • 1
  • 8
  • 16