简体   繁体   中英

SpringBoot Application cannot find defined Repository in Controller

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM