0% found this document useful (0 votes)
27 views10 pages

Intro To SpringBoot and Rest and JPA

The document describes code for a Spring Boot application that manages patient data using RESTful web services. It includes a PatientController class annotated with @RestController to handle HTTP requests. The controller uses a PatientService interface to delegate data access logic. Implementations of PatientDAO and PatientService interfaces perform data persistence and service operations by interacting with an in-memory list of Patient objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views10 pages

Intro To SpringBoot and Rest and JPA

The document describes code for a Spring Boot application that manages patient data using RESTful web services. It includes a PatientController class annotated with @RestController to handle HTTP requests. The controller uses a PatientService interface to delegate data access logic. Implementations of PatientDAO and PatientService interfaces perform data persistence and service operations by interacting with an in-memory list of Patient objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction Spring Boot

i-design
---------------------------
POST and Get Using @RequestMapping

PatientController
-----------------------------------------------------------------------------------
-
package com.springboot.controller;

import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import com.springboot.dao.PatientDAO;
import com.springboot.domain.Patient;
import com.springboot.service.PatientService;

//Fill the code


@RestController
public class PatientController {
//Fill the code

@Autowired
private PatientService patientService;

@RequestMapping(value="/patient/list",method=RequestMethod.GET)
public List<Patient> listPatients(){
//Fill the code
return patientService.getPatients();

//Fill the code


@RequestMapping(value="/patient/create",method=RequestMethod.POST)
public List<Patient> createPatient(@RequestBody Patient patient){
//Fill the code
return patientService.createPatient(patient);
}

}
--------------------------------------------------------------------
PatientDAOImpl

package com.springboot.dao;

import java.util.List;
import java.util.ArrayList;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import com.springboot.domain.Patient;

//Fill the code


@Repository
public class PatientDAOImpl implements PatientDAO {

static List<Patient> patientList = new ArrayList<>();

static {
patientList.add(new Patient(1L, "Swathy", "9876567234",
"[email protected]", "31-07-1989"));
patientList.add(new Patient(2L, "Vanmathi", "9873877234",
"[email protected]", "23-04-1992"));
patientList.add(new Patient(3L, "Kevin", "9823641234",
"[email protected]", "01-04-2000"));
}

public List<Patient> createPatient(Patient p){


//Fill the code
patientList.add(p);
return patientList;

public List<Patient> getPatients(){


//Fill the code
return patientList;
}

}
---------------------------------------------------------------------
PatientServiceImpl
package com.springboot.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springboot.dao.PatientDAO;
import com.springboot.dao.PatientDAOImpl;
import com.springboot.domain.Patient;

//Fill the code


@Service
public class PatientServiceImpl implements PatientService {

// Fill the code


@Autowired
private PatientDAO dao;

public List<Patient> createPatient(Patient p) {


// Fill the code
return dao.createPatient(p);

public List<Patient> getPatients() {


// Fill the code
return dao.getPatients();
}

-----------------------------------------------------------------------------------
-------------------------------------------------------
Intro to SpringBoot
I-assess
-----------------------------------------------------------------------------------
-------------------------------------------------------
package com.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.dao.DoctorDAO;
import com.springboot.domain.Doctor;
import com.springboot.service.DoctorService;
import com.springboot.service.DoctorServiceImpl;

//Fill your code here


@RestController
public class DoctorController {
//Fill your code here

@Autowired
private DoctorService service;

@GetMapping("/doctor/list")
public List<Doctor> getDoctors(){
//Fill your code here
return service.list();
}
}
-----------------------------------------------------------------------------
package com.springboot.dao;

import java.util.*;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.springboot.domain.Doctor;

//Fill your code here


@Repository
public class DoctorDAOImpl implements DoctorDAO {

private static List<Doctor> doctors = new ArrayList<>();

static{
doctors.add(new Doctor(1, "Elizabeth", "MBBS", 4.2, "Cardiologist",
750.));
doctors.add(new Doctor(2, "Michael", "MBBS", 2.0, "Dermatologist",
1500.));
doctors.add(new Doctor(3, "Charlotte", "MBBS", 3.1, "Pediatrics",
200.));
doctors.add(new Doctor(4, "Lucas", "BDS", 1.9, "Dentist", 250.));
}

public List<Doctor> list() {


//Fill your code here
return doctors;
}

-------------------------------------------------------
package com.springboot.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springboot.dao.DoctorDAO;
import com.springboot.dao.DoctorDAOImpl;
import com.springboot.domain.Doctor;

//Fill your code here


@Service
public class DoctorServiceImpl implements DoctorService{

//Fill your code here


@Autowired
private DoctorDAO dao;

public List<Doctor> list(){

//Fill your code here


return dao.list();
}

===================================================================================
=======================================================
JPA and Rest
i-assess
package com.weather.jpa.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.RestController;
import com.weather.jpa.domain.WeatherReport;
import com.weather.jpa.service.WeatherService;

//Fill your code here


@RestController
public class WeatherController {

//Fill your code here


@Autowired
private WeatherService service;

@GetMapping("/weatherReport")
public List<WeatherReport> getData() {
//Fill your code here
return service.getData();
}

//Fill your code here


@PostMapping("/weatherReport")
public WeatherReport addCases(@RequestBody WeatherReport cases) {
//Fill your code here
return service.addCases(cases);

//Fill your code here


@PutMapping("/weatherReport")
public WeatherReport updateCases(@RequestBody WeatherReport cases) {
//Fill your code here
return service.updateCases(cases);

//Fill your code here


@GetMapping("weatherReport/{id}")
public WeatherReport view(@PathVariable Long id) {
//Fill your code here
return service.view(id);

//Fill your code here


@DeleteMapping("weatherReport/{id}")
public Boolean deleteCases(@PathVariable Long id) {
//Fill your code here
return service.deleteCases(id);

}
----------------------------------------------------------------------------
package com.weather.jpa.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "weather_report")
public class WeatherReport {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "city")
private String city;
@Column(name = "min_temperature")
private Double minTemperature;
@Column(name = "max_temperature")
private Double maxTemperature;

public WeatherReport() {
}

public WeatherReport(Long id, String city, Double minTemperature, Double


maxTemperature) {
super();
this.id = id;
this.city = city;
this.minTemperature = minTemperature;
this.maxTemperature = maxTemperature;
}

public Long getId() {


return id;
}

public void setId(Long id) {


this.id = id;
}

public String getCity() {


return city;
}

public void setCity(String city) {


this.city = city;
}

public Double getMinTemperature() {


return minTemperature;
}

public void setMinTemperature(Double minTemperature) {


this.minTemperature = minTemperature;
}

public Double getMaxTemperature() {


return maxTemperature;
}

public void setMaxTemperature(Double maxTemperature) {


this.maxTemperature = maxTemperature;
}
}
---------------------------------------------------------
package com.weather.jpa.repository;
import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Repository;
import com.weather.jpa.domain.WeatherReport;
import org.springframework.data.repository.CrudRepository;

//Fill your code here


@Repository("weatherRepository")
public interface WeatherRepository extends CrudRepository<WeatherReport,Long>{

----------------------------------------------------------
package com.weather.jpa.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.weather.jpa.domain.WeatherReport;
import com.weather.jpa.repository.WeatherRepository;

//Fill your code here


@Service
public class WeatherService {
//Fill your code here
@Autowired
private WeatherRepository repo;

public List<WeatherReport> getData() {


//Fill your code here
return (List<WeatherReport>) repo.findAll();

public WeatherReport addCases(WeatherReport cases) {


//Fill your code here
return repo.save(cases);

public WeatherReport updateCases(WeatherReport cases) {


//Fill your code here
return repo.save(cases);

public WeatherReport view(Long id) {


//Fill your code here
Optional<WeatherReport> optional=repo.findById(id);
return optional.get();

}
public Boolean deleteCases(Long id) {
//Fill your code here
if(repo.findById(id).get()!=null) {
repo.deleteById(id);
return true;
}

return false;

}
-----------------------------------------------------------------------------------
-------------------------------------------------------
Rset and JPA
i-design
-----------------------------------------------------------------------------------
-------------------------------------------------------
package com.springboot.controller;

import com.springboot.dao.DoctorDAO;
import com.springboot.domain.Doctor;
import com.springboot.service.DoctorService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//Fill your code here


@RestController
@RequestMapping(value="/doctor")
public class DoctorController
{
//Fill your code here
@Autowired
private DoctorService service;

@GetMapping("/list")
List<Doctor> getDoctors()
{
//Fill your code here
return service.list();

//Fill your code here


@PostMapping("/create")
Boolean create(@RequestBody Doctor doctor)
{
//Fill your code here
return service.save(doctor);

}
}
-------------
package com.springboot.dao;

import com.springboot.domain.Doctor;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

//Fill your code here


@Repository
public class DoctorDAOImpl implements DoctorDAO
{
static ArrayList<Doctor> doctorList = new ArrayList<Doctor>();

static
{
doctorList.add(new
Doctor(1,"Harinii","MBBS",4.2,"Orthologist",750.00));
doctorList.add(new
Doctor(2,"Nithin","MBBS",2.0,"Gynecologist",1500.00));
}

public Boolean save(Doctor doctor)


{
//Fill your code here
return doctorList.add(doctor);
}

public List<Doctor> list()


{
//Fill your code here
return doctorList;

}
}

------------------------------
package com.springboot.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springboot.dao.DoctorDAO;
import com.springboot.dao.DoctorDAOImpl;
import com.springboot.domain.Doctor;

//Fill your code here


@Service
public class DoctorServiceImpl implements DoctorService{

//Fill your code here


@Autowired
private DoctorDAO dao;
public Boolean save(Doctor doctor)
{
//Fill your code here
return dao.save(doctor);

public List<Doctor> list()


{
//Fill your code here
return dao.list();

You might also like