小编典典

“字段需要找不到类型的bean。” 使用mongodb的错误Spring Restful API

spring

我的项目目录结构是这样的。

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文件时,它会抛出此错误。

申请启动失败

描述:

main.java.rest.UsersController中的userService字段需要找不到“ main.java.service.UserService”类型的Bean。

行动:

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

不确定出什么问题后,我开始四处搜寻,发现需要包含Beans.xml文件并在其中注册userService。我做到了,但是没有用。我真的很陌生,所以我对发生的事情一无所知。


阅读 792

收藏
2020-04-12

共1个答案

小编典典

解决了。因此,默认情况下,@SpringBootApplication将扫描所有属于声明范围的软件包。

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

因此,基于此问题,有两种方法可以做到这一点。

@SpringBootApplication(scanBasePackages={
"com.example.something", "com.example.application"})

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

因此,我使用第二种方法,即重组我的软件包,它起作用了!现在我的包结构变成了这样。

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
2020-04-12