@ExceptionHandler Example in Spring Boot

In this post, we will see how to use @ExceptionHandler annotation for exception handling purposes using Spring Boot.

@ExceptionHandler Example in Spring Boot

Let’s see some basic points about @ExceptionHandler annotations, later we will see a complete example using Spring Boot. These annotations used for error/exception handling purpose.

@ExceptionHandler Annotation : –

  • This annotation introduced in Spring 3.0 and available in org.springframework.web.bind.annotation package.
  • @ExceptionHandler used with the method if we try to use with the class it will show compilation error.
  • When we use @ExceptionHandler only for exception handling, then in each controller we need to define Exception handling logic(We will see later in the example).

We will see an example using @ExceptionHandler only. Later we have an example using @ControllerAdvice and @ExceptionHandler. The problem with @ExceptionHandler is we can’t handle exception globally only using this one.

Let’s see an example where we will use @ExceptionHandler only for exception handling purpose.

@ExceptionHandler Example in Spring Boot.

Step 1 – open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)click on next.

Step 2 –  Fill all details as below and click on finish.

 

Step 3 – Modify pom.xml

 

@ExceptionHandler Example in Spring Boot

 

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>exceptionHandlerexample</groupId>
  <artifactId>exceptionHandlerexample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>exceptionHandlerexample</name>
  
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>

		</dependency>

	</dependencies>

	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>

			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<fork>true</fork>
					<executable>C:\Program Files\Java\jdk1.8.0_131\bin\javac.exe</executable>
				</configuration>
			</plugin>


		</plugins>
	</build>
</project>

Directory structure –

@ExceptionHandler Example in Spring Boot

@ExceptionHandler Example in Spring Boot

Book.java

package com.errorhandling;

public class Book {
	String bookId;
	String bookName;
	String bookPrice;



	public String getBookId() {
		return bookId;
	}

	public void setBookId(String bookId) {
		this.bookId = bookId;
	}

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public String getBookPrice() {
		return bookPrice;
	}

	public void setBookPrice(String bookPrice) {
		this.bookPrice = bookPrice;
	}

}

Story.java

package com.errorhandling;


public class Story {

	private String storyId;
	private String storyName;
	public String getStoryId() {
		return storyId;
	}
	public void setStoryId(String storyId) {
		this.storyId = storyId;
	}
	public String getStoryName() {
		return storyName;
	}
	public void setStoryName(String storyName) {
		this.storyName = storyName;
	}

	
	
}

BookController.java

package com.errorhandling;

import javax.validation.valueextraction.ExtractedValue;

import org.springframework.boot.ApplicationArguments;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@Controller				
@RequestMapping(value = "/book")
public class BookController {
	
	@RequestMapping(value = "/test",method = RequestMethod.POST)
	@ResponseBody
    public Book getBook(@RequestBody Book book) {
		
    	if(book.getBookId() == null) {
    		throw new RecordNotFoundException("Record not found");
    	}
		return book;
	}
	
	
	@ExceptionHandler(RecordNotFoundException.class)
	@ResponseBody
	public ResponseError handleCustomException(RecordNotFoundException ex) {
		ResponseError responseError = new ResponseError();
		responseError.setErrorMessage(ex.getMessage());
		responseError.setStatusCode(HttpStatus.NOT_FOUND.value());
		return responseError;

	}

}

StoryController.java

package com.errorhandling;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller				
@RequestMapping(value = "/story")
public class StroyController {
	@RequestMapping(value = "/test",method = RequestMethod.POST)
	@ResponseBody
    public Story getBook(@RequestBody Story story) {
		
    	if(story.getStoryId()== null) {
    		throw new RecordNotFoundException("Record not found");
    	}
		return story;
	}
	
	
	@ExceptionHandler(RecordNotFoundException.class)
	@ResponseBody
	public ResponseError handleCustomException(RecordNotFoundException ex) {
		ResponseError responseError = new ResponseError();
		responseError.setErrorMessage(ex.getMessage());
		responseError.setStatusCode(HttpStatus.NOT_FOUND.value());
		return responseError;

	}
}

RecordNotFoundException.java

package com.errorhandling;

public class RecordNotFoundException extends RuntimeException {
	String message;

	public RecordNotFoundException() {
		super();
	}

	public RecordNotFoundException(String message) {
		super(message);
		this.message = message;
	}
}

ResponseError.java

package com.errorhandling;

public class ResponseError {
	private String errorMessage;
	private int statusCode;

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public int getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(int statusCode) {
		this.statusCode = statusCode;
	}



}

Let’s see @ExceptionHandler Example in Spring Boot SpringMain class example.

SpringMain.java

package com.errorhandling;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringMain {
	public static void main(final String[] args) {
		final ConfigurableApplicationContext configurableApplicationContext = SpringApplication
				.run(SpringMain.class, args);
		 
		 
	}
}

 

We are done. Let’s run and deploy the application.

 

Now our @ExceptionHandler Example in Spring Boot in the almost the final stage.

Request data to test BookController’s API – http://localhost:9093/book/test. We are intentionally providing bookId as null so that it will throw an exception and we will have error response.

{
"bookName":"Premchand's best stories",
"bookId":null,
"bookPrice":"10"
}

@ExceptionHandler Example in Spring Boot

 

Request data to test StoryController’s API – http://localhost:9093/story/test

{
"storyName":"Push ki Rat",
"storyId":null
}

@ExceptionHandler Example in Spring Boot

 

That’s all about @ExceptionHandler Example in Spring Boot. We have seen how to use @ExceptionHandler for exception handling. Did you notice that in BookController and StroyController we same code for exception handling i.e “Record not found” is common error message for both cases? Suppose we have a hundred controllers and we have to show similar kind of error message then we need to define same code in each controller that we might don’t want. We can resolve this problem using @ControllerAdvice annotation, see an example here.

 

You may like –

@ExceptionHandler Docs.