Exception Handling in Spring Boot

In this blog, I have posted, how can we do the exception handling in spring boot micro service. And also see the sample code for global exception handling. Before you start this blog. You should know how to create a spring boot project. If you do not have an idea bout it. Please refer this link https://samplecoder.com/how-to-create-a-spring-boot-application-in-sts/

Exception Handling in Spring Boot Micro Service is an important concept. This blog really helps you for interviews. If you are a spring boot developer or if you want to become a spring boot developer, you should know this concept.

Exception Handling

Exception handling the general term. Normally exception is an unexpected behavior, it breaks the normal flow of your program. You should handle it to continue the normal flow of your program. In spring boot you can handle the exception using try and catch block as how you are doing in normal Java programs. And also we can handle the exception globally. This additional feature added in spring framework.

To handle the exception globally, you have to follow the below steps,

  • Create a custom exception class
  • Create a class and annotate it with @ControllerAdvice or @RestControllerAdvice annotation
  • Define some method to handle the exception in the Advice Controller class. And all those methods should be annotated with @Exceptionhadler annotations.

Create the custom exception

package com.samplecoder.emo.exceptions;

public class StudentNotFoundException extends RuntimeException{
	public StudentNotFoundException(String message) {

Create Advice Controller

package com.samplecoder.emo.handler;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;

import com.samplecoder.emo.exceptions.StudentNotFoundException;

public class StudentExceptionsHandlers {

	public ResponseEntity<String> studentNotFound(RuntimeException ex, WebRequest request){
		return new ResponseEntity<String>(ex.getMessage(), HttpStatus.NOT_FOUND);

Now just throw Student Not Found Exception from where ever you want the exception. In this sample code I have throwing the exception in Student controller

package com.samplecoder.emo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.samplecoder.emo.entity.Student;
import com.samplecoder.emo.exceptions.StudentNotFoundException;
import com.samplecoder.emo.repository.StudentRepository;

public class StudentController {
	private StudentRepository studentRepository;
	private Student get(@PathVariable(name = "id", required = true) Integer id) {
		Student student = studentRepository.findById(id).orElse(null);
		if(student == null)
			throw new StudentNotFoundException("Student not found");
		return student;

Postman response

Request: http://localhost:8080/api/student/get/10
Response: Student not found


If you are here, I believe you read this blog completely. Now you can answer to below questions

  • What is an exception?
  • How to handle the exception spring boot micro service?
  • How to handle the exception in globally?