Home > Java > Spring MVC Exception Handling Dengan @ControllerAdvice Annotation

Spring MVC Exception Handling Dengan @ControllerAdvice Annotation

Spring MVC menyediakan cara yang mudah untuk meng-handle exception dan error. @ExceptionHandler annotation merupakan core untuk feature handling exception dan error. Untuk setiap controller kita dapat mendefinisikan sebuah metode yang secara otomatis dipanggil jika exception dan error pada controller terjadi.

Contoh.

@ExceptionHandler(ArithmeticException.class)
public String getArithmeticEception(ArithmeticException e) {
/* Login in here */
return "error";
}

Method tersebut akan dieksekusi saat exception dengan type ArithmeticException terjadi di controller dimana method tersebut didefinisikan. Untuk membuat method tersebut dieksekusi untuk exception dengan type ArithmeticException yang terjadi di semua controller makan digunakan @ControllerAdvice annotation.

Berikut ini example lengkapnya.

Langkah 1. Buat JSP view untuk memilih jenis exception ( ExceptionPage.jsp )

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Exception Page</title>
</head>
<body>
Exception Example <br>
<a href="SQLException.do">SQLException Example</a> <br>
<a href="<%=request.getContextPath()%>/RuntimeException.do">RuntimeException Example</a> <br>
<a href="<%=request.getContextPath()%>/ArithmeticException.do">ArithmeticException Example</a>
</body>
</html>

Exception Page

Exception Page

Buat JSP view untuk error message ( ExceptionErrorPage.jsp )

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Exception Error Page</title>
</head>
<body>
<h2>Ops! Something went wrong</h2>
<h2><b>${errorMessage}</b></h2>
</body>
</html>

Exception Error Message

Exception Error Message

Langkah 2 : Buat Spring controller ( ExceptionController.java )

import java.sql.SQLException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ExceptionController {
public boolean exceptionFlag;
public String errorMessage;

@RequestMapping(value="/SQLException.do", method= RequestMethod.GET)
public String testSQLException() throws SQLException{
exceptionFlag = true;
errorMessage  = "SQL Exception";

if(exceptionFlag == true){
throw new SQLException(errorMessage );
}

return null;
}

@RequestMapping(value="/RuntimeException.do", method= RequestMethod.GET)
public String testRuntimeException() throws RuntimeException{
exceptionFlag = true;
errorMessage  = "Runtime Exception";

if(exceptionFlag == true){
throw new RuntimeException(errorMessage);
}
return null;
}

@RequestMapping(value="/ArithmeticException.do", method= RequestMethod.GET)
public String testArithmeticException() throws ArithmeticException{
exceptionFlag = true;
errorMessage  = "Arithmetic Exception";

if(exceptionFlag == true){
throw new ArithmeticException(errorMessage);
}
return null;
}
}

controller ini mapping request dari link di JSP view yang telah kita buat sebelumnya. Method pada controller ini hanya untuk membuat exception.

Buat Spring controller yang akan meng-handle jika exception terjadi ( ExceptionControllerAdvice.java )

package com.mvc.controller;

import java.sql.SQLException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ExceptionControllerAdvice {

@ExceptionHandler(SQLException.class)
public ModelAndView getSQLException(SQLException e){

ModelAndView mdv = new ModelAndView("ExceptionErrorPage");
mdv.addObject("errorMessage", e.getMessage());

return mdv;
}

@ExceptionHandler(RuntimeException.class)
public ModelAndView getRuntimexception(RuntimeException e){

ModelAndView mdv = new ModelAndView("ExceptionErrorPage");
mdv.addObject("errorMessage", e.getMessage());

return mdv;
}

@ExceptionHandler(ArithmeticException.class)
public ModelAndView getArithmeticEception(ArithmeticException e){

ModelAndView mdv = new ModelAndView("ExceptionErrorPage");
mdv.addObject("errorMessage", e.getMessage());

return mdv;
}
}


Controller tersebut secara sederhana hanya terdiri @ExceptionHandler dan @ControllerAdvice annotation.

Referensi : dari berbagai sumber.
Semoga bermanfaat

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: