我有一個正在處理的應用程式,我將它連接到我的 mongoDB 資料庫和所有內容,但是執行 findAll 或 findById 方法總是回傳一個空括號 {}
我認為它“有效”,因為我的 mongodb 資料庫中共有 5731 條記錄,并且在執行“findAll()”時它回傳 5731 個開括號。
我做了一些研究,發現了一些類似的帖子,但大多數人說治愈我的收藏是正確的(確實如此)。
這是我的自定義變數類“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
我的控制器檔案
@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);
}
}
和我的存盤庫
public interface StockRepository extends MongoRepository<stockIncome, String> {
}
有什么想法可以幫助我除錯嗎?
uj5u.com熱心網友回復:
固定的!
解決方案是在我的變數建構式中將 public 添加到 id 。
改變了
string id
到
public string id
現在它不再是空的!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/373477.html
