“字段需要一个无法找到的类型的 bean."使用mongodb的错误spring restful API [英] 'Field required a bean of type that could not be found.' error spring restful API using mongodb

查看:20
本文介绍了“字段需要一个无法找到的类型的 bean."使用mongodb的错误spring restful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在几周内一直在学习 Spring,一直在关注本教程

So I've been learning Spring in the couples of week, been following this tutorial

构建 RESTful Web 服务

一切都很好,直到我尝试将其集成到 mongodb.所以我跟着这个教程.

All was well until I tried to integrate it to mongodb. So I follow this tutorial.

使用 MongoDB 访问数据

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

But my practice is partially still using the first one. So my project directory structure is like this.

src/
├── main/
│   └── java/
|       ├── model/
|       |   └── User.java
|       ├── rest/
|       |   ├── Application.java
|       |   ├── IndexController.java
|       |   └── UsersController.java
|       └── service/
|           └── UserService.java
└── resources/
    └── application.properties

这是我的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;
    }
}

这是我的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();
    }
}

这是我的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();
}

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

I could compile them (I'm using gradle for compilation because I'm following the tutorial), but when I run the jar file it was throwing this error.

应用程序无法启动

说明:

main.java.rest.UsersController 中的字段 userService 需要一个 bean输入找不到的main.java.service.UserService".

Field userService in main.java.rest.UsersController required a bean of type 'main.java.service.UserService' that could not be found.

操作:

考虑定义一个'main.java.service.UserService'类型的bean您的配置.

Consider defining a bean of type 'main.java.service.UserService' in your configuration.

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

Not sure what is wrong I start googling around and found that I need to include Beans.xml file and register the userService in it. I did that but it's not working. I'm really new to this so I really have no clue on what's going on.

推荐答案

我也有同样的错误:

***************************
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

我的包的构建方式与接受的答案中提到的方式相同.我通过在主类中添加 EnableMongoRepositories 注释来解决我的问题,如下所示:

And my packages were constructed in the same way as mentioned in the accepted answer. I fixed my issue by adding EnableMongoRepositories annotation in the main class like this:

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

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

如果你需要添加多个不要忘记花括号:

If you need to add multiple don't forget the curly braces:

@EnableMongoRepositories(basePackageClasses
    = {
        MSASMSRepository.class, APartyMappingRepository.class
    })

这篇关于“字段需要一个无法找到的类型的 bean."使用mongodb的错误spring restful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆