| 1234567891011121314151617181920212223242526272829 |
- package com.example.projectback.controllers;
- import com.example.projectback.model.Student;
- import com.example.projectback.repository.StudentRepository;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.*;
- import java.util.List;
- @RestController
- @CrossOrigin(origins = "http://localhost:4200")
- @RequestMapping("/students")
- public class StudentController {
- private final StudentRepository studentRepository;
- @Autowired
- public StudentController(StudentRepository studentRepository) {this.studentRepository = studentRepository;}
- @GetMapping
- public List<Student> getAllStudents() {return studentRepository.findAll();}
- @PostMapping
- public ResponseEntity<Student> addStudent(@RequestBody Student student) {
- studentRepository.save(student);
- return new ResponseEntity<>(student, HttpStatus.CREATED);
- }
- }
|