我正在Spring MVC中创建一个项目,我需要使用全局异常处理,我正在使用@controlleradvice和@exceptionhandler。问题是我每次抛出异常时都会显示正常的Tomcat异常,而不是显示我的自定义错误页面。
我的全局控制器的代码如下:
package com.mphasis.insurance.controller;
import java.sql.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ControllerAdvice
@EnableWebMvc
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler
{
@ExceptionHandler(value=RuntimeException.class)
public ModelAndView handle(RuntimeException ex)
{
System.out.println("Runtime Exception Occured");
return new ModelAndView("RuntimeException");
}
@ExceptionHandler(value=NullPointerException.class)
public ModelAndView handleNullPointerException(NullPointerException ex)
{
System.out.println("Null Pointer Exception Occured");
return new ModelAndView("NullPointerException");
}
@ExceptionHandler(value=ArrayIndexOutOfBoundsException.class)
public ModelAndView handleArrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException ex)
{
System.out.println("Array Index Out Of BoundsException Occured");
return new ModelAndView("ArrayIndexOutOfoundException");
}
@ExceptionHandler(value=ArithmeticException.class)
public ModelAndView handleArithmeticException(ArithmeticException ex)
{
System.out.println("Arithmetic Exception Occured");
return new ModelAndView("ArithmeticException");
}
@ExceptionHandler(value=SQLException.class)
public ModelAndView handleSQLException(SQLException ex)
{
System.out.println("SQL Exception Occured");
return new ModelAndView("SQLException");
}
@ExceptionHandler(value=NumberFormatException.class)
public ModelAndView handleNumberFormatException(NumberFormatException ex)
{
System.out.println("Number Format Exception Occured");
return new ModelAndView("NumberFormatException");
}
}