Spring Boot — Exception Handling | Code Factory

Code Factory
2 min readApr 28, 2020

--

Reference Link : Link

Donate : Link

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.codeFactory</groupId>
<artifactId>spring-boot-exception-handling</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-exception-handling</name>
<description>Demo project for Spring Boot Exception Handling</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

SpringBootExceptionHandlingApplication.java

package com.codeFactory;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author code.factory
*
*/
@SpringBootApplication
public class SpringBootExceptionHandlingApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootExceptionHandlingApplication.class, args);
}
}

Employee.java

package com.codeFactory.bean;import java.util.HashMap;
import java.util.Map;
/**
* @author code.factory
*
*/
public class Employee {
private int id;
private String name;
private static Map<Integer, Employee> employeeList = new HashMap<Integer, Employee>();
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public static Map<Integer, Employee> getEmployeesList() {
employeeList.put(1, new Employee(1, "Emp1"));
employeeList.put(2, new Employee(2, "Emp2"));
employeeList.put(3, new Employee(3, "Emp3"));
return employeeList;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}

EmployeeController.java

package com.codeFactory.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.codeFactory.bean.Employee;
import com.codeFactory.exception.EmployeeNotFound;
/**
* @author code.factory
*
*/
@RestController
public class EmployeeController {
@GetMapping("getEmployee/{id}")
public Employee getEmployee(@PathVariable("id") int id) {
if(Employee.getEmployeesList().containsKey(id)) {
return Employee.getEmployeesList().get(id);
}
throw new EmployeeNotFound("Employee Not Found");
}
}

EmployeeExceptionController.java

package com.codeFactory.exception;import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
/**
* @author code.factory
*
*/
@ControllerAdvice
public class EmployeeExceptionController {
@ExceptionHandler(value = EmployeeNotFound.class)
public ResponseEntity<Object> exception(EmployeeNotFound e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
}

EmployeeNotFound.java

package com.codeFactory.exception;/**
* @author code.factory
*
*/
public class EmployeeNotFound extends RuntimeException {
public EmployeeNotFound(String message) {
super(message);
}
}

Hit URL : http://localhost:8080/getEmployee/1

Hit URL : http://localhost:8080/getEmployee/4

--

--