简体   繁体   English

Springboot Mongodb findAll() 和 findbyId 返回空大括号

[英]Springboot Mongodb findAll() and findbyId returns empty curly brackets

I have an application that I am working on, I have it connecting to my mongoDB database and everything, but doing either a findAll or findById method always returns an empty brackets {}我有一个正在开发的应用程序,我让它连接到我的 mongoDB 数据库和所有内容,但是执行 findAll 或 findById 方法总是返回一个空括号{}

I think its "working" because I have a total of 5731 records in my mongodb database, and when doing a "findAll()" it returns 5731 open brackets.我认为它“工作”,因为我的 mongodb 数据库中共有 5731 条记录,并且在执行“findAll()”时它返回 5731 个左括号。

I did some research and found some similar posts but most said to make cure my collection is correct (which it is).我做了一些研究,发现了一些类似的帖子,但大多数人说要治愈我的收藏是正确的(确实如此)。

here is my custom variable class "stockIncome.java"这是我的自定义变量 class “stockIncome.java”

@Document(collection = "IncomeStatement")
public class stockIncome {
    
    @Id
    String id;

spring.data.mongodb.uri=mongodb+srv://XXX_XXX_XXX(Hiding my username/password/hostname)?retryWrites=true&w=majority

my controller file我的 controller 文件

@RestController
public class stockController {

    public StockRepository stockRepository;

    public stockController(StockRepository stockRepository) {
        this.stockRepository = stockRepository; 
    }
    
    @GetMapping("/all")
    public List<stockIncome> findStocks(){  
        return stockRepository.findAll();
    }
    @GetMapping("/stocks/{id}")
    public Optional<stockIncome> findStock(@PathVariable final String id){
        return stockRepository.findById(id);
    }
}

and my repository和我的存储库

public interface StockRepository extends MongoRepository<stockIncome, String> {    
}

any ideas to help me debug this?有什么想法可以帮助我调试吗?

Fixed!固定的!

the solution was to add public to the id in my variable constructor.解决方案是在我的变量构造函数中将 public 添加到 id 。

changed改变了

string id to public string id string idpublic string id

and now its no longer empty!现在它不再是空的了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM