Home > Enterprise >  Springboot Mongodb findAll() and findbyId returns empty curly brackets
Springboot Mongodb findAll() and findbyId returns empty curly brackets

Time:12-03

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 {}

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.

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"

@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

@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?

CodePudding user response:

Fixed!

the solution was to add public to the id in my variable constructor.

changed

string id to public string id

and now its no longer empty!

  • Related