0% found this document useful (0 votes)
21 views5 pages

Mini Project Rest API Unit - 4

The document outlines a mini project for a Student Management System using Spring Boot and JPA, focusing on CRUD operations and RESTful API endpoints. It includes functional requirements, code examples for the model, repository, service, and controller layers, and specifies the API endpoints for managing student records. The project aims to facilitate the creation, retrieval, updating, and deletion of student information.

Uploaded by

230822.cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Mini Project Rest API Unit - 4

The document outlines a mini project for a Student Management System using Spring Boot and JPA, focusing on CRUD operations and RESTful API endpoints. It includes functional requirements, code examples for the model, repository, service, and controller layers, and specifies the API endpoints for managing student records. The project aims to facilitate the creation, retrieval, updating, and deletion of student information.

Uploaded by

230822.cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MINI PROJECT UNIT – 4

QUESTION

 Project 1: Student Management System with Spring Boot (CRUD + RESTful API).

Overview :

 This project builds a Student Management System using Spring Boot + JPA. It introduces CRUD
operations and prepares the base for relational mapping.

Functional Requirements

 Create, read, update, and delete student records.


 Expose all operations via REST API.
 Endpoints
 POST /students → Add new student.
 GET /students → Get all students.
 GET /students/{id} → Get student by ID.
 PUT /students/{id} → Update student.
 DELETE /students/{id} → Delete student.

CODE :

A. Model Layer: Student.java

package com.example.springapp.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String department;
private String email;

// Constructors
public Student() {}

public Student(String name, String department, String email) {


this.name = name;
this.department = department;
this.email = email;
}

// Getters and Setters


public Long getId() { return id; }
public void setId(Long id) { this.id = id; }

public String getName() { return name; }


public void setName(String name) { this.name = name; }

public String getDepartment() { return department; }


public void setDepartment(String department) { this.department = department; }

public String getEmail() { return email; }


public void setEmail(String email) { this.email = email; }
}

B. Repository Layer: StudentRepository.java


package com.example.springapp.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import com.example.springapp.model.Student;

public interface StudentRepository extends JpaRepository<Student, Long> {


// No need to write SQL queries — JPA handles CRUD automatically
}

C. Service Layer: StudentService.java


package com.example.springapp.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import com.example.springapp.model.Student;
import com.example.springapp.repository.StudentRepository;

@Service
public class StudentService {

@Autowired
private StudentRepository repository;

public Student addStudent(Student student) {


return repository.save(student);
}

public List<Student> getAllStudents() {


return repository.findAll();
}

public Student getStudentById(Long id) {


return repository.findById(id).orElse(null);
}

public Student updateStudent(Long id, Student studentDetails) {


Student student = repository.findById(id).orElse(null);
if (student != null) {
student.setName(studentDetails.getName());
student.setDepartment(studentDetails.getDepartment());
student.setEmail(studentDetails.getEmail());
return repository.save(student);
}
return null;
}

public String deleteStudent(Long id) {


repository.deleteById(id);
return "Student deleted successfully!";
}
}

D. Controller Layer: StudentController.java

package com.example.springapp.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.example.springapp.model.Student;
import com.example.springapp.service.StudentService;

@RestController
@RequestMapping("/students")
public class StudentController {

@Autowired
private StudentService service;

@PostMapping
public Student createStudent(@RequestBody Student student) {
return service.addStudent(student);
}

@GetMapping
public List<Student> getAllStudents() {
return service.getAllStudents();
}

@GetMapping("/{id}")
public Student getStudentById(@PathVariable Long id) {
return service.getStudentById(id);
}

@PutMapping("/{id}")
public Student updateStudent(@PathVariable Long id, @RequestBody Student student) {
return service.updateStudent(id, student);
}
@DeleteMapping("/{id}")
public String deleteStudent(@PathVariable Long id) {
return service.deleteStudent(id);
}
}

OUTPUT :

[
{
"id": 1,
"name": "John Doe",
"department": "Computer Science",
"email": "[email protected]"
}
]

You might also like