19- Build Create Employee REST API

 

Image 1

Note - Model  and Repository layer completed


Service Class Updates

springboot-backendd--> src/main/java--> net.javaguides.springboot.service--> EmployeeService.java

package net.javaguides.springboot.service;

import java.util.List;

import net.javaguides.springboot.model.Employee;

public interface EmployeeService {
   
    Employee saveEmployee(Employee employee);
}


ServiceImpl Class Updates

springboot-backendd--> src/main/java-->  net.javaguides.springboot.service.impl--> EmployeeServiceImpl.java

package net.javaguides.springboot.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import net.javaguides.springboot.exception.ResourceNotFound;
import net.javaguides.springboot.model.Employee;
import net.javaguides.springboot.repository.EmployeeRepository;
import net.javaguides.springboot.service.EmployeeService;


@Service
public class EmployeeServiceImpl implements EmployeeService {

    private EmployeeRepository employeeRepository;
   
    public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
        super();
        this.employeeRepository = employeeRepository;
    }

    @Override
    public Employee saveEmployee(Employee employee) {
    return employeeRepository.save(employee);
    }
}


Controller Class Updates

springboot-backendd--> src/main/java--> net.javaguides.springboot.controller--> EmployeeController.java

package net.javaguides.springboot.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import net.javaguides.springboot.model.Employee;
import net.javaguides.springboot.service.EmployeeService;

@RestController
@RequestMapping("/api/employees")
public class EmployeeController {

    private EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        super();
        this.employeeService = employeeService;
    }
   
   
   
    // build create employee REST API
    // http://localhost:8080/api/employees-------  POST request on Postman App
   
    @PostMapping()
    public ResponseEntity<Employee> saveEmployee(@RequestBody Employee employee){
       
        return new ResponseEntity<Employee>(employeeService.saveEmployee(employee),HttpStatus.CREATED);
    }
    }

Comments

Popular posts from this blog

INDEX OF Zeek Spring Boot