Create the SpringBootHelloWorldApplication.java as below-
package com.javainuse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorldApplication.class, args);
}
}
Create the Employee model class as follows-
package com.javainuse.model;
public class Employee {
private String empId;
private String name;
private String designation;
private double salary;
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
}
We first define the classes in the service package.
Define the EmployeeServiceException to thrown by the EmployeeService class in case of exceptionss.
package com.javainuse.service;
public class EmployeeServiceException extends Exception {
private static final long serialVersionUID = -470180507998010368L;
public EmployeeServiceException() {
super();
}
public EmployeeServiceException(final String message) {
super(message);
}
}
Define the service class. We have defined three methods in it for explaining 3 scenarios.
package com.javainuse.service;
import org.springframework.stereotype.Service;
import com.javainuse.model.Employee;
@Service
public class EmployeeService {
//return employee object
public Employee getEmployee() throws EmployeeServiceException {
Employee emp = new Employee();
emp.setName("emp1");
emp.setDesignation("manager");
emp.setEmpId("1");
emp.setSalary(3000);
return emp;
}
//return employee as null
public Employee getEmployeeNull() throws EmployeeServiceException {
return null;
}
//throw exception
public Employee getEmployeeException() throws EmployeeServiceException {
throw new EmployeeServiceException();
}
}
Now we define the Controller package classes
Define the ResourceNotFoundException class. This Exception will be thrown by the controller when no resource i.e. employee to be returned in our case is found.
package com.javainuse.exceptionhandling;
public class ResourceNotFoundException extends Exception {
private static final long serialVersionUID = -9079454849611061074L;
public ResourceNotFoundException() {
super();
}
public ResourceNotFoundException(final String message) {
super(message);
}
}
Next we define the controller class
@RequestMapping maps /employee request to return an employee object.
We have two other requestmappings, one returns a null employee and the other throws an exception.
Also the EmployeeController has the EmployeeService instance autowired.
package com.javainuse.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.javainuse.exceptionhandling.ResourceNotFoundException;
import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;
import com.javainuse.service.EmployeeServiceException;
@RestController
public class EmployeeController {
@Autowired
EmployeeService employeeService;
//Happy path, an employee is returned as response
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public Employee getEmpl() throws ResourceNotFoundException, EmployeeServiceException {
try {
Employee emp = employeeService.getEmployee();
if (emp == null) {
throw new ResourceNotFoundException("Employee not found");
}
return emp;
} catch (EmployeeServiceException e) {
throw new EmployeeServiceException("Internal Server Exception while getting exception");
}
}
//no employee found so ResourceNotFoundException is thrown
@RequestMapping(value = "/employee2", method = RequestMethod.GET)
public Employee getEmp2() throws ResourceNotFoundException, EmployeeServiceException {
try {
Employee emp = employeeService.getEmployeeNull();
if (emp == null) {
throw new ResourceNotFoundException("Employee not found");
}
return emp;
} catch (EmployeeServiceException e) {
throw new EmployeeServiceException("Internal Server Exception while getting exception");
}
}
//Some exception is thrown by service layer
@RequestMapping(value = "/employee3", method = RequestMethod.GET)
public Employee getEmp3() throws ResourceNotFoundException, EmployeeServiceException {
try {
Employee emp = employeeService.getEmployeeException();
if (emp == null) {
throw new ResourceNotFoundException("Employee not found");
}
return emp;
} catch (EmployeeServiceException e) {
throw new EmployeeServiceException("Internal Server Exception while getting exception");
}
}
}