0

Hi I am following this guide for creating API. I have similar code to what is in this guide. Now I am trying to create another API within the same Project with the data stored in a separate collection. But Whenever I make any API calls, it always uses the same database. I cannot figure out where the database location is configured. Can I please get help on finding out where I can configure the database connectons to my Java program? Here is my code.

@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api/v1")
public class TicketController {
    @Autowired
    private TicketRepository ticketRepository;
    @Autowired
    private TicketGeneratorService ticketGeneratorService;
    
    @GetMapping("/tickets")
    @ApiOperation(value = "Retrieves all Tickets in the database")
    public List<Ticket> getAllTickets() {
        return ticketRepository.findAll();
    }
    
    @PostMapping("/tickets/create")
    @ApiOperation(value = "Adds a Ticket to the database")
    public Ticket createTicket(@Valid @RequestBody Ticket ticket) {
        ticket.setId(ticketGeneratorService.generateSequence(Ticket.SEQUENCE_NAME));
        return ticketRepository.save(ticket);
    }   
    
}

Here is my application.properties file

This is the code I have under my application.properties file. I do not understand how this can be incorrect. employeeDatabase is the Database I am using, and inside it I am trying to use two different collections. All the data I create and retrieve from only comes from one of the collections. How can I get access to the other collection?

# MONGODB (MongoProperties)
spring.data.mongodb.uri=mongodb://localhost:27017/employeeDatabase

employeeDatabase is the database which includes the collections that I am using.

Here is a list of my classes. enter image description here

Madhu Sharma
  • 562
  • 4
  • 9

1 Answers1

1

Check your application.properties for the name of the database, chances are you forgot to change the name from the first API.

If you wish to have multiple mongoDBs in your project, check this answer for guidance.

If you are using only one database, but documents get written to the same collection of the first API, chances are you didn't change the model class in the second API.

Create a new model for a new collection:

import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "collection_name")
public class CollectionName { ... }

And use this model for writing to the database now.

Ahmed Hammad
  • 2,798
  • 4
  • 18
  • 35