StudentController.java 1000 B

1234567891011121314151617181920212223242526272829
  1. package com.example.projectback.controllers;
  2. import com.example.projectback.model.Student;
  3. import com.example.projectback.repository.StudentRepository;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.bind.annotation.*;
  8. import java.util.List;
  9. @RestController
  10. @CrossOrigin(origins = "http://localhost:4200")
  11. @RequestMapping("/students")
  12. public class StudentController {
  13. private final StudentRepository studentRepository;
  14. @Autowired
  15. public StudentController(StudentRepository studentRepository) {this.studentRepository = studentRepository;}
  16. @GetMapping
  17. public List<Student> getAllStudents() {return studentRepository.findAll();}
  18. @PostMapping
  19. public ResponseEntity<Student> addStudent(@RequestBody Student student) {
  20. studentRepository.save(student);
  21. return new ResponseEntity<>(student, HttpStatus.CREATED);
  22. }
  23. }