开发者社区 问答 正文

Controller.Controller中的错误字段存储库需要找不到“ Controller.Re

我试图在一个Spring应用程序中将我的Controller连接到存储库,但是却收到一条错误消息:“在TweetsController.TweetsController中的Field tweetRepository需要一个类型为'TweetsController.TweetRepository'的bean。”

有人可以帮我弄这个吗?提前致谢。我还附带了代码示例。

TwitterApplication.java

package SpringMVC.Twitter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@ComponentScan("AuthController")
@ComponentScan("TweetsController")
public class TwitterApplication {

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

TwitterController.java

package TweetsController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;

@RestController
public class TweetsController {

    @Autowired
    private TweetRepository tweetRepository;

    @RequestMapping("/tweets")
    public Iterable<TweetsContent> getAllTweets() {
        return tweetRepository.findAll();
    }

    @RequestMapping("tweet/{id}")
    public Optional<TweetsContent> getTweet(@PathVariable int id) {
        return tweetRepository.findById(id);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/tweets")
    public boolean addTweet(@RequestBody TweetsContent tweet) {
        TweetsContent t = tweetRepository.save(new TweetsContent(tweet.getTitle(), tweet.getContent()));
        if (t != null)
            return true;
        else
            return false;
    }
}

TwitterRepository.java

package TweetsController;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TweetRepository extends CrudRepository<TweetsContent, Integer> { }

展开
收起
垚tutu 2019-11-29 19:58:47 695 分享 版权
阿里云 AI 助理回答

从你提供的代码片段来看,问题可能出在几个地方。首先,确保你的TweetRepository接口正确地指定了泛型类型参数,以匹配你的实体类(在这里假设是TweetsContent)。其次,确认Spring能够扫描到这个接口。根据你的描述和代码,我提供以下修改建议:

  1. 修正TweetRepository的泛型:你需要指定泛型类型参数,以便Spring Data JPA知道你要操作的是哪个实体类。
// 修改TweetRepository.java
package TweetsController;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import TweetsController.TweetsContent; // 确保导入正确的实体类

@Repository
public interface TweetRepository extends CrudRepository<TweetsContent, Integer> { 
    // 注意这里指定了<TweetsContent, Integer>,其中Integer是主键类型
}
  1. 确保包扫描路径正确:在TwitterApplication.java中,你使用了@ComponentScan注解来指定要扫描的包。但是,通常情况下,当使用@SpringBootApplication时,它会自动扫描同级及子包下的组件,所以额外的@ComponentScan可能不是必须的,除非你的包结构不在默认扫描范围内。如果你的TweetsControllerTweetRepository位于不同的层级或非标准位置,请确保它们被正确包含在扫描路径内。如果它们都在SpringMVC.Twitter的子包下,可以考虑移除显式的@ComponentScan注解,或者确保路径正确无误。

  2. 检查实体类:虽然没有提供TweetsContent的代码,但请确保该类有正确的JPA注解,比如@Entity, @Id等,并且与数据库表映射正确。

  3. 配置数据源:确保你的应用配置中包含了对数据库连接的正确配置,因为Spring Data JPA需要一个有效的数据源来操作数据库。

完成上述修改后,重新启动你的Spring Boot应用,看看错误是否解决。如果问题依旧,请检查是否有其他配置错误或依赖冲突,并提供更多信息以便进一步分析。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答分类:
问答地址: