Rest Api Using Mongo DB

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 3

1) Create spring boot project with following dependencies

a) spring-boot-starter-data-mongodb
b) spring-boot-starter-web
c) spring-boot-devtools
d) lombok
e) spring-boot-starter-test

2) Set mongodb properties


spring.data.mongodb.database=wiprodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.repositories.enabled=true

3) Create model class


import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Data
@Document("student")
public class Student {

@Id
private Integer studentid;
private String studentname;
private Float studentpercentage;

4) Create respository interface


import com.wipro.model.Student;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import java.util.List;

public interface StudentRepository extends MongoRepository<Student,Integer> {

@Query("{studentname:'?0'}")
List<Student> findStudentByStudentname(String studentname);

@Query("studentpercentage : { $gte : '?0'}}")


List<Student> findByPercentageGreaterThan(Float studentpercentage);

5) Create service class


import com.wipro.model.Student;
import com.wipro.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StudentService {

@Autowired
private StudentRepository studentRepository;

public Student addStudent(Student student){


studentRepository.save(student);
return student;
}
public Student updateStudent(Student student,Integer studentid){
student.setStudentid(studentid);
studentRepository.save(student);
return student;
}

public Student deleteStudent(Integer studentid){


Student student = null;
Optional<Student> optional = studentRepository.findById(studentid);
if(optional.isPresent()) {
student = optional.get();
studentRepository.deleteById(studentid);
}
return student;
}

public Student getStudentById(Integer studentid){


Student student = null;
Optional<Student> optional = studentRepository.findById(studentid);
if(optional.isPresent()) {
student = optional.get();
}
return student;
}

public List<Student> getAllStudent(){


return studentRepository.findAll();
}

public List<Student> getAllStudentsByName(String studentname){


return studentRepository.findStudentByStudentname(studentname);
}

public List<Student> getStudentsByPercentage(Float studentpercentage){


return studentRepository.findByPercentageGreaterThan(studentpercentage);
}
}

6) Create controller class


import com.wipro.model.Student;
import com.wipro.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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);
}
}

You might also like