Rest Api Using Mongo DB
Rest Api Using Mongo DB
Rest Api Using Mongo DB
a) spring-boot-starter-data-mongodb
b) spring-boot-starter-web
c) spring-boot-devtools
d) lombok
e) spring-boot-starter-test
@Data
@Document("student")
public class Student {
@Id
private Integer studentid;
private String studentname;
private Float studentpercentage;
import java.util.List;
@Query("{studentname:'?0'}")
List<Student> findStudentByStudentname(String studentname);
import java.util.List;
import java.util.Optional;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
import java.util.List;
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@PostMapping
public Student addStudent(@RequestBody() Student student){
return studentService.addStudent(student);
@PutMapping("/{studentid}")
public Student updateStudent(@RequestBody() Student student, @PathVariable() Integer studentid){
return studentService.updateStudent(student,studentid);
}
@DeleteMapping("/{studentid}")
public Student deleteStudent(@PathVariable() Integer studentid){
return studentService.deleteStudent(studentid);
}
@GetMapping("/{studentid}")
public Student getStudentById(@PathVariable() Integer studentid){
return studentService.getStudentById(studentid);
}
@GetMapping
public List<Student> getAllStudents(){
return studentService.getAllStudent();
}
@GetMapping("/name/{studentname}")
public List<Student> getStudentsByName(@PathVariable() String studentname){
return studentService.getAllStudentsByName(studentname);
}
@GetMapping("/percentage/{studentpercentage}")
public List<Student> getStudentsByPercentageGreaterThen(@PathVariable() Float studentpercentage){
return studentService.getStudentsByPercentage(studentpercentage);
}
}