I have created my first Spring boot project with 3.2.1 Springboot version and JPA with MySQL database. I am trying to get the table using REST API

145 views Asked by At

The table is created but the data is not getting inserted inside the table. The queries show a success of the insert statements but couldn't get the data in output.

Please help me as this is my first ever Spring boot application as I am new to this.

Many Thanks in advance :)

Here is my stack trace statement: enter image description here

Here is my application.properties file:

application.properties

I tried changing the application.properties file based on Stack overflow suggestions after reading a few posts but it didn't work. Initially I was not even able to create a table but after I saw in stack overflow that we need to change the Dialect properties in application.properties as per SQL version, table was created but now data is not getting inserted. [Table created and even used REST API to call it and it's successful. (https://i.stack.imgur.com/jDqkF.png)

1

There are 1 answers

1
Hadi Kattan On
  1. You have to define a repository that extends JpaRepository. Example:

    @Repository
     public interface StudentRepository extends JpaRepository<Student, String> {
    
     }
    
  2. Define a service class and use the repo already defined to insert a student, as shown below:

      @RequiredArgsConstructor
      @Service
      public class StudentService {
    
      private final StudentRepository studentRepo;
      public void insert(Student student) {
         studentRepo.save(student);
      }
    
      }
    
  3. Define a controller class and expose an API endpoint to add students.

Example:

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(path = "/v1/students")
public class StudentController {
private final StudentService studentService;
@PostMapping
    public ResponseEntity<Student> newStudent(@RequestBody @Valid Student student) {
        return ResponseEntity.ok(studentService.insert(student));
    }

}

Make sure you're connecting to the appropriate database and using the correct table name.