gpt4 book ai didi

java - ...NoteController 中的字段存储库需要类型为 ...NoteRepository 的 bean,但无法找到

转载 作者:行者123 更新时间:2023-12-02 06:09:33 24 4
gpt4 key购买 nike

我尝试执行一个Spring项目,但总是返回错误,不知道是否是Spring Boot版本的问题。

我有两个项目:

  • IntegrationCore(Spring Boot 项目);
  • IntegrationModel(仅适用于模型);

项目 IntegrationCore 包含项目 IntegrationModel 的引用(构建路径 -> 项目)

当我执行项目 IntegrationCore(运行方式 -> Spring boot 应用程序)时,返回控制台错误:


APPLICATION FAILED TO START


Description:

Field repository in com.integration.controller.NoteController required a > bean of type 'com.integration.repository.NoteRepository' that could not be > found. Action: Consider defining a bean of type 'com.integration.repository.NoteRepository' > in your configuration.

我尝试过:

  • 将 Spring boot 版本从 1.5.7 更改为 1.5.20;
  • 在Application类中包含mongoDb的注解;
  • 插入注释以引用 IntegrationCore 包;

集成核心

 src/
├── main/
│ └── java/
| ├── com.integration/
| | └── IntegrationCoreApplication.java
| ├── com.integration.controller/
| | └── NoteController.java
| └── com.integration.repository/
| └── NoteRepository.java

集成模型

 src/
├── main/
│ └── java/
| ├── com.integrationModel/
| | └── NoteStatuses.java
| └── com.integrationModel.filter/
| └── NoteStatusesParams.java

IntegrationCoreApplication.java

package com.integration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages={
"com.integration", "com.integrationModel"})
public class IntegrationCoreApplication {

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

}

NoteController.java

package com.integration.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.integrationModel.NoteStatuses;
import com.integrationModel.filter.NoteStatusesParams;
import com.integration.repository.NoteRepository;

@Component
@RestController
@RequestMapping(value = "/services/nota")
public class NoteController {

@Autowired
private NoteRepository repository;

@RequestMapping(value = "/getStatuses", method = RequestMethod.POST)
@ResponseBody
public List<NoteStatuses> getStatuses(@RequestBody List<NoteStatusesParams> params) {
return repository.findStatuses(params);
}

}

NoteRepository.java

package com.integration.repository;

import java.util.List;

import org.springframework.stereotype.Service;

import com.integrationModel.NoteStatuses;
import com.integrationModel.filter.NoteStatusesParams;

@Service
public interface NoteRepository {
List<NoteStatuses> findStatuses(List<NoteStatusesParams> param);
}

NoteStatusesParams.java

package com.integrationModel.filter;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Singular;

import java.util.List;

@Getter
@Builder(builderClassName = "Builder")
@NoArgsConstructor
@AllArgsConstructor
public class NoteStatusesParams {
@Singular
private List<String> orderNumbers;
private String providerCpfCnpj;
private String noteNumber;
}

NoteStatuses.java

package com.integrationModel;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

@Data
@EqualsAndHashCode(of = {"id"})
public class NoteStatuses {

private Boolean physicalReceipt;
...
}

最佳答案

问题在于您的以下定义。

@Service
public interface NoteRepository {
List<NoteStatuses> findStatuses(List<NoteStatusesParams> param);
}

它是一个接口(interface),您不能使用@Service 将其定义为服务。 Spring 将尝试为此 bean 创建一个实例,但因此失败。

您需要使用 @Repostiry 注释并扩展相关存储库(即 JPA 或 ElasticSearch 存储库)将其定义为存储库。

关于java - ...NoteController 中的字段存储库需要类型为 ...NoteRepository 的 bean,但无法找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55920471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com