Spring unified exception handling problems @ControllerAdvice not in force

 

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
//处理异常(全局异常处理)
import org.springframework.web.bind.annotation.RestController;


@ControllerAdvice

public class HandleExceptionAdvice {
@ExceptionHandler(Exception.class)
public String error(Exception ex,Model model) {
	model.addAttribute("errorMsg", ex.getMessage());
	return "commons/error";
}
}

 

SpringMVC used today to achieve a global exception handling (the code above) by @ControllerAdvice comment, but the debugger for a long time have reported 500 errors, finally imagine is not because spring is simply not scanned @ControllerAdvice comment

This file is MVC.XML

This is the structure of the package

Then adjusted his place HandleExceptionAdvice located, to the following package cn.as.web

Found on the success of

So use @ControllerAdvice global exception handling must be scanned to spring! ! !

Released six original articles · won praise 3 · Views 791

Guess you like

Origin blog.csdn.net/SmileLucki/article/details/105387585