'字段需要一个无法找到的bean类型.使用mongodb错误弹簧restful API

Eka*_*nto 68 java rest spring mongodb

所以我一直在学习一周中的Spring,一直在学习本教程

构建RESTful Web服务

一切顺利,直到我试图将它整合到mongodb.所以我按照本教程.

使用MongoDB访问数据

但我的做法部分仍在使用第一个.所以我的项目目录结构是这样的.

src/
??? main/
?   ??? java/
|       ??? model/
|       |   ??? User.java
|       ??? rest/
|       |   ??? Application.java
|       |   ??? IndexController.java
|       |   ??? UsersController.java
|       ??? service/
|           ??? UserService.java
??? resources/
    ??? application.properties
Run Code Online (Sandbox Code Playgroud)

这是我的model/User.java文件

package main.java.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection="user")
public class User {

    private int age;
    private String country; 
    @Id
    private String id;
    private String name;


    public User() {
        super();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的rest/UsersController.java文件

package main.java.rest;

import java.util.List;
import main.java.service.UserService;
import main.java.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/users")
public class UsersController {

    @Autowired
    UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public List<User> getAllUsers() {
        return userService.findAll();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的service/UserService.java文件

package main.java.service;

import java.util.List;
import main.java.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface UserService extends MongoRepository<User, String> {
    public List<User> findAll();
}
Run Code Online (Sandbox Code Playgroud)

我可以编译它们(我正在使用gradle进行编译,因为我正在按照教程),但是当我运行jar文件时,它抛出了这个错误.


应用程序未能启动


描述:

main.java.rest.UsersController中的字段userService需要一个无法找到的类型为'main.java.service.UserService'的bean.

行动:

考虑在配置中定义类型为'main.java.service.UserService'的bean.

不确定有什么问题我开始google搜索并发现我需要包含Beans.xml文件并在其中注册userService.我这样做但是没有用.我真的很陌生,所以我真的不知道发生了什么.

Eka*_*nto 117

解决了它.因此,默认情况下,@SpringBootApplication将扫描属于声明的所有包.

假设我的主类ExampleApplication具有@SpringBootApplication声明是在内部声明的com.example.something,那么所有掉入的组件都会com.example.something被扫描而com.example.applicant不会被扫描.

因此,基于这个问题,有两种方法可以做到这一点.使用

@SpringBootApplication(scanBasePackages={
"com.example.something", "com.example.application"})
Run Code Online (Sandbox Code Playgroud)

这样,应用程序将扫描所有指定的组件,但我想如果规模越来越大怎么办?

所以我使用第二种方法,重组我的包,它工作!现在我的包结构变成了这样.

src/
??? main/
?   ??? java/
|       ??? com.example/
|       |   ??? Application.java
|       ??? com.example.model/
|       |   ??? User.java
|       ??? com.example.controller/
|       |   ??? IndexController.java
|       |   ??? UsersController.java
|       ??? com.example.service/
|           ??? UserService.java
??? resources/
    ??? application.properties
Run Code Online (Sandbox Code Playgroud)


小智 52

@Service在service/UserService.java中添加.


mum*_*asa 37

我遇到了同样的问题,我@Autowired从控制器中删除了注释。如果您的存储库是一个类,那么需要自动装配注释来使用该存储库,但根据我的经验,当它是一个接口时,您不需要添加注释@Autowired

  • 我的存储库类是一个接口。我尝试了这个,从 Repository 类的声明中删除 @Autowired ,但我得到了一个空指针异常。还需要执行其他操作来实例化存储库的实例吗? (5认同)

Ahm*_*URI 16

您必须将@Service注释添加到服务的实现中。


sha*_*bby 12

我也有同样的错误:

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

Description:

Field repository in com.kalsym.next.gen.campaign.controller.CampaignController required a bean of type 'com.kalsym.next.gen.campaign.data.CustomerRepository' that could not be found.


Action:

Consider defining a bean of type 'com.kalsym.next.gen.campaign.data.CustomerRepository' in your configuration.de here
Run Code Online (Sandbox Code Playgroud)

我的包装的构造方式与接受的答案中提到的方式相同.我通过在主类中添加EnableMongoRepositories注释修复了我的问题,如下所示:

@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = CustomerRepository.class)
public class CampaignAPI {

    public static void main(String[] args) {
        SpringApplication.run(CampaignAPI.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)


Aka*_*ppa 12

我遇到了同样的问题,我所要做的就是将应用程序放在比服务,dao和域包高一级的包中.


Ash*_*ngh 9

我遇到过同样的问题。我的错误是我@Service在服务接口上使用了注释。该@Service注释应该应用于 ServiceImpl 类。


Bhu*_*kar 8

该线程现在已经很旧了,但是我正在发布我的答案,这可能对其他人有用。

我遇到过同样的问题。原来,在其他模块中还有另一个同名的类。我重命名了该类,它解决了这个问题。


Vit*_*min 6

由于自动导入而花费了很多时间。为什么@Service从Intellij Idea导入import org.jvnet.hk2.annotations.Service;而不是import org.springframework.stereotype.Service;

  • 您从我的答案中不了解什么?尝试运行项目时遇到了与该问题相同的错误。这是因为我从错误的库中导入了“服务”。 (2认同)
  • @mannedear我想我的第一条评论也会回答您的评论。 (2认同)

Hea*_*ren 6

通常我们可以从两个方面来解决这个问题:

  1. Spring Boot扫描bean时应使用适当的注释,例如@Component
  2. 扫描路径包括,就像所有其他上面提到的类。

顺便说一句,对于@Component、@Repository、@Service 和@Controller之间的区别,有一个很好的解释。


小智 6

在你的dao类中添加@Repository

例子:

@Repository
public class DaoClassName implements IIntefaceDao {
}
Run Code Online (Sandbox Code Playgroud)


God*_*ava 6

为了创建 bean 并注入它,类应该用@Componet、@service 或 @Repository 中的任何一个标记,在您的上下文中它应该是

package main.java.service;

import java.util.List;
import main.java.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;
@Repository
public interface UserService extends MongoRepository<User, String> {
    public List<User> findAll();
}
Run Code Online (Sandbox Code Playgroud)


eri*_*egz 6

对于任何通过谷歌搜索通用 bean 错误消息来到这里,但实际上试图通过客户端接口上的注释将假客户端添加到其 Spring Boot 应用程序的人@FeignClient来说,上述解决方案都不适合您。

要解决此问题,您需要将@EnableFeignClients注释添加到您的 Application 类中,如下所示:

@SpringBootApplication
// ... (other pre-existing annotations) ...
@EnableFeignClients // <------- THE IMPORTANT ONE
public class Application {
Run Code Online (Sandbox Code Playgroud)

这样,修复与@EnableMongoRepositories上面提到的修复类似。遗憾的是,这个通用错误消息需要针对每种情况进行量身定制的修复...


dur*_*ell 5

spring-boot-starter-data-jpa 如果使用 Spring Boot,则必须作为依赖项导入