diff --git a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/controller/StudentController.java b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/controller/StudentController.java index 75920f8..3085399 100644 --- a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/controller/StudentController.java +++ b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/controller/StudentController.java @@ -5,14 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import com.wasim.rest.api.entity.Student; import com.wasim.rest.api.service.StudentService; @@ -30,19 +23,24 @@ public ResponseEntity createStudent(@RequestBody Student student) { return new ResponseEntity(studentService.createStudent(student), HttpStatus.CREATED); } - @GetMapping("/getAllStudent") + @GetMapping("/getAllStudents") public ResponseEntity> getAllStudent(){ return new ResponseEntity>(studentService.getAllStudent(), HttpStatus.OK); } + + @GetMapping("/getStudent/{id}") + public ResponseEntity getStudentById(@PathVariable long id){ + return new ResponseEntity(studentService.getStudentById(id), HttpStatus.OK); + } @PutMapping("/editStudent/{id}") - public ResponseEntity updateStudent(@PathVariable("id") long id, @RequestBody Student student){ + public ResponseEntity updateStudent(@PathVariable long id, @RequestBody Student student){ return new ResponseEntity(studentService.updateStudent(id, student), HttpStatus.OK); } @DeleteMapping("/deleteStudent/{id}") - public ResponseEntity deleteStudent(@PathVariable("id") long id){ + public ResponseEntity deleteStudent(@PathVariable long id){ studentService.deleteStudent(id); return new ResponseEntity(HttpStatus.OK); } diff --git a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/entity/Student.java b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/entity/Student.java index 8c5c985..5c89a08 100644 --- a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/entity/Student.java +++ b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/entity/Student.java @@ -17,12 +17,4 @@ public class Student { private String name; private String email; private String address; - - - - - - - - } diff --git a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/exception/GlobalExceptionHandler.java b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..9930a74 --- /dev/null +++ b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/exception/GlobalExceptionHandler.java @@ -0,0 +1,17 @@ +package com.wasim.rest.api.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ProblemDetail; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class GlobalExceptionHandler { + @ExceptionHandler(StudentNotFoundException.class) + public ProblemDetail handleStudentNotFoundException(StudentNotFoundException e){ + ProblemDetail problem = ProblemDetail.forStatus(HttpStatus.NOT_FOUND); + problem.setTitle("Student not found."); + problem.setProperty("Student id: ", e.getStudentId()); + return problem; + } +} diff --git a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/exception/StudentNotFoundException.java b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/exception/StudentNotFoundException.java new file mode 100644 index 0000000..3cfe588 --- /dev/null +++ b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/exception/StudentNotFoundException.java @@ -0,0 +1,12 @@ +package com.wasim.rest.api.exception; + +public class StudentNotFoundException extends RuntimeException{ + private final Long studentId; + public StudentNotFoundException(Long studentId){ + super("Student with ID " + studentId + " was not found."); + this.studentId = studentId; + } + public Long getStudentId() { + return studentId; + } +} diff --git a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/repository/StudentRepository.java b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/repository/StudentRepository.java index 13a80b4..3f9a7ed 100644 --- a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/repository/StudentRepository.java +++ b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/repository/StudentRepository.java @@ -6,4 +6,6 @@ public interface StudentRepository extends JpaRepository{ + Student getStudentById(long id); + } diff --git a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/service/StudentService.java b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/service/StudentService.java index 54dac01..eb3a7f8 100644 --- a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/service/StudentService.java +++ b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/service/StudentService.java @@ -7,9 +7,11 @@ public interface StudentService { public Student createStudent(Student student); - + public List getAllStudent(); - + + public Student getStudentById(long id); + public Student updateStudent(long id, Student student); public void deleteStudent(long id); diff --git a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/service/StudentServiceImpl.java b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/service/StudentServiceImpl.java index a764d48..768c4e6 100644 --- a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/service/StudentServiceImpl.java +++ b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/service/StudentServiceImpl.java @@ -2,6 +2,7 @@ import java.util.List; +import com.wasim.rest.api.exception.StudentNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -25,6 +26,14 @@ public List getAllStudent() { return studentRepository.findAll(); } + @Override + public Student getStudentById(long id){ + if(!studentRepository.existsById(id)){ + throw new StudentNotFoundException(id); + } + return studentRepository.getStudentById(id); + } + @Override public Student updateStudent(long id, Student student) { student.setId(id);