i'm following a tutorial. We need to create a REST API to do basic crud operations on a student object. I just wanted to add a get by id.
Here is my controller:
package com.example.demo.student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping(path="api/student")
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public List<Student> getStudents(){
return studentService.getStudents();
}
@GetMapping(path="{studentId}")
public Optional<Student> getStudent(@PathVariable("studentId")Long studentId){
return studentService.getStudent(studentId);
}
@PostMapping
public void registerNewStudent(@RequestBody Student student){
studentService.addNewStudent(student);
}
@DeleteMapping(path="{studentId}")
public void deleteStudent(@PathVariable("studentId")Long studentId){
studentService.deleteStudent(studentId);
}
// only update name
@PutMapping(path="{studentId}")
public void updateStudent(
@PathVariable("studentId")Long studentId,
@RequestParam(required = false) String name)
{
studentService.updateStudent(studentId, name );
}
}
and my service :)
package com.example.demo.student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@Service
public class StudentService {
private final StudentRepository studentRepository;
@Autowired
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
public List<Student> getStudents(){
return studentRepository.findAll();
}
public void addNewStudent(Student student) {
Optional<Student> studentByName = studentRepository.findStudentByName(student.getName());
if(studentByName.isPresent()){
try {
throw new Exception("name taken");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
studentRepository.save(student);
}
public void deleteStudent(Long studentId) {
boolean exists = studentRepository.existsById(studentId);
if (!exists){
throw new RuntimeException("student with id" + studentId + " does not exist.");
}
studentRepository.deleteById(studentId);
}
@Transactional
public void updateStudent(Long studentId, String name) {
Student student = studentRepository.findById(studentId).orElseThrow(() -> new RuntimeException(
"student with Id" + studentId + " does not exist"
)
);
if(name != null && name.length() > 0 && !Objects.equals(student.getName(), name)){
student.setName(name);
}
}
public Optional<Student> getStudent(Long studentId) {
Student student = studentRepository.findById(studentId).orElseThrow(() -> new RuntimeException(
"student with Id" + studentId + " does not exist"
)
);
return studentRepository.findById(studentId);
}
}
Nevertheless, when I test .../api/student/1, I've this error: "error": "Method Not Allowed",
Can anyone help me ?
Thanks guys :) Have a nice day