Introduction to Spring Boot and RESTful APIs
Spring Boot is a powerful framework for building Java applications, particularly RESTful APIs. In this tutorial, we will guide you through a complete Spring Boot RESTful API CRUD project,This course is designed for beginners, so no prior Spring Boot experience is necessary.
Crud function how to perform the RESTful Web Service let discuss with following annotations.
@PostMapping: annotation which used to create new record.
@GetMapping: annotation which used to reads a record.
@RequestMapping: annotation which used to search the record.
@PutMapping: annotation which used to update the existing record.
@DeleteMapping: annotation which used to delete the record.
Lets Started Project
First Step go to Spring Initializer to configure the project using the following dependencies.
1. Spring Data JPA
2. Spring Web
3. Lombok
4. MySQL Driver
Create the Package entity
Inside the Package create the class Teacher
@Entity @Table(name ="teacher") public class Teacher { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="teacher_id",length = 11) private int teacherid; @Column(name="teacher_name",length = 45) private String teachername; @Column(name="phone",length = 11) private String phone; //constructor public Teacher(int teacherid, String teachername, String phone) { this.teacherid = teacherid; this.teachername = teachername; this.phone = phone; } public Teacher(String teachername, String phone) { this.teachername = teachername; this.phone = phone; } public Teacher() { } public int getTeacherid() { return teacherid; } public void setTeacherid(int teacherid) { this.teacherid = teacherid; } public String getTeachername() { return teachername; } public void setTeachername(String teachername) { this.teachername = teachername; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "Teacher{" + "teacherid=" + teacherid + ", teachername='" + teachername + '\'' + ", phone='" + phone + '\'' + '}'; }
Create the Package DTO
Inside the Package create the class TeacherDTO and TeacherSaveDTO and TeacherUpdateDTO
TeacherDTO
@AllArgsConstructor @NoArgsConstructor @Data public class TeacherDTO { private int teacherid; private String teachername; private String phone; }
TeacherSaveDTO
@AllArgsConstructor @NoArgsConstructor @Data public class TeacherSaveDTO { private String teachername; private String phone; }
TeacherUpdateDTO
@AllArgsConstructor @NoArgsConstructor @Data public class TeacherUpdateDTO { private int teacherid; private String teachername; private String phone; }
Create the Package Controller
nside the Package create the class TeacherController
@RestController @CrossOrigin @RequestMapping("api/v1/teacher") public class TeacherController { @Autowired private TeacherService teacherService; @PostMapping("/save") public String saveTeacher(@RequestBody TeacherSaveDTO teacherSaveDTO) { String teachername = teacherService.addTeacher(teacherSaveDTO); return teachername; } @GetMapping("/getAllTeachers") public List<TeacherDTO> getAllTeachers() { List<TeacherDTO> allTeachers = teacherService.getAllTeachers(); return allTeachers; } @PutMapping("/update") public String updateTeacher(@RequestBody TeacherUpdateDTO teacherUpdateDTO) { String teachername = teacherService.updateTeacher(teacherUpdateDTO); return "updateee"; } @DeleteMapping("/delete/{id}") public String deleteTeacher(@PathVariable(value = "id")int id) { boolean deleteTeacher = teacherService.deleteTeacher(id); return "deleteeeedd"; } }
Teacher Service
Inside the Package create the interface TeacherService.java and TeacherServiceIMPL.java.
TeacherService
public interface TeacherService { String addTeacher(TeacherSaveDTO teacherSaveDTO); List<TeacherDTO> getAllTeachers(); String updateTeacher(TeacherUpdateDTO teacherUpdateDTO); boolean deleteTeacher(int id); }
TeacherServiceIMPL
@Service public class TeacherServiceIMPL implements TeacherService { @Autowired private TeacherRepo teacherRepo; @Override public String addTeacher(TeacherSaveDTO teacherSaveDTO) { Teacher teacher = new Teacher( teacherSaveDTO.getTeachername(), teacherSaveDTO.getPhone() ); teacherRepo.save(teacher); return teacher.getTeachername(); } @Override public List<TeacherDTO> getAllTeachers() { List<Teacher> getTeacher = teacherRepo.findAll(); List<TeacherDTO> teacherDTOList = new ArrayList<>(); for(Teacher teacher:getTeacher) { TeacherDTO teacherDTO = new TeacherDTO( teacher.getTeacherid(), teacher.getTeachername(), teacher.getPhone() ); teacherDTOList.add(teacherDTO); } return teacherDTOList; } @Override public String updateTeacher(TeacherUpdateDTO teacherUpdateDTO) { if(teacherRepo.existsById(teacherUpdateDTO.getTeacherid())) { Teacher teacher = teacherRepo.getById(teacherUpdateDTO.getTeacherid()); teacher.setTeachername(teacherUpdateDTO.getTeachername()); teacher.setPhone(teacherUpdateDTO.getPhone()); teacherRepo.save(teacher); return teacher.getTeachername(); } else { System.out.println("Teacher ID not Found"); } return null; } @Override public boolean deleteTeacher(int id) { if(teacherRepo.existsById(id)) { teacherRepo.deleteById(id); } else { System.out.println("Teacher ID not Found"); } return false; }
TeacherRepo
public interface TeacherRepo extends JpaRepository<Teacher,Integer> { }