Search code examples
javaspring-bootspring-data-rest

how to use a post method with a spring data rest process?


I'm using a repostiroy rest resource service and currently I need to implement a method to import the data from a excel file into my database. I don't see how I can do with spring repository rest resource? since it's an interface. do I use restController or does it have a way to do both?


Solution

  • The standard flow would be:

    1. Create a repository file

      @Repository
      public interface RepositoryClass extends JpaRepository<T, ID>{}
      
    • where T is the class and ID is the data type of the ID (ex: Long, Integer etc.)

    The JpaRepository already have save() and saveAll() methods(1)

    1. In service create a method that read the data and saves it in the database

      @Service
      public class ServiceClass{
      
      // ...some code
      
         public void importData(// ... parameters){
              // open the excel file and import the data in an ArrayList for exemple
      
              repositoryClass.saveAll(arrayWithData);
         }
      }
      
    2. The last step is to create a endpoint in the controller which calls the method that is written in service.