@ControllerAdvice异常处理程序方法未被调用

rac*_*ana 6 java spring controller exception-handling spring-mvc

我正在为Spring MVC中的异常处理示例演示应用程序.我正在尝试 Exception Handling With @ControllerAdvice

我按照链接中描述的步骤进行操作.

但是,当我运行我的应用程序时,我得到以下错误

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.test.core.ApplicationException
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅我正在处理的课程

ApplicationException.java

public class ApplicationException extends RuntimeException{


/**
 * 
 */
private static final long serialVersionUID = -9061684361606685158L;
private ErrorStatus errorStatus;
private int msgNumber;
private Object []args;

public ApplicationException(){}

public ApplicationException(ErrorStatus errorStatus, int msgNumber,
        Object[] args) {
    this.errorStatus = errorStatus;
    this.msgNumber = msgNumber;
    this.args = args;
  }
    //getter setters
}
Run Code Online (Sandbox Code Playgroud)

控制器类

ApplicationExceptionController.java

 @Controller
@RequestMapping("/exception")
public class ApplicationExceptionController {

@RequestMapping(value="/error",method=RequestMethod.GET)
@ResponseBody
public void helloException() 
{
    throw new ApplicationException(ErrorStatus.NotFound,1,new Object[]{"Website"});

  //here ErrorStatus is a enum class
}  
}
Run Code Online (Sandbox Code Playgroud)

这是GlobalExceptionHandler类

GlobalExceptionHandler.java

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler
public void notFount(){
    System.out.println("----------CaughtApplicationException-----------");
}
}
Run Code Online (Sandbox Code Playgroud)

调度员servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"  
xmlns:p="http://www.springframework.org/schema/p"      
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:mvc="http://www.springframework.org/schema/mvc" 

xsi:schemaLocation=
    "http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


<context:component-scan  base-package="com.test.controller,com.test.core" />
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="prefix" value="/WEB-INF/jsp/" />  
    <property name="suffix" value=".jsp" />   
</bean>  

 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
     <list> <ref bean="jsonConverter"/></list>
    </property>
</bean>

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
     <property name="supportedMediaTypes" value="application/json" />
</bean>
Run Code Online (Sandbox Code Playgroud)

我在上面运行的应用程序使用 的http://本地主机:8080/SpringExceptionHandling /异常/错误此网址

但我得到以下例外

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.test.core.ApplicationException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

com.test.core.ApplicationException
com.test.controller.ApplicationExceptionController.helloException(ApplicationExceptionController.java:19)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Run Code Online (Sandbox Code Playgroud)

我通过@ControllerAdvice异常处理程序方法没有被调用此链接但仍然遇到同样的问题请帮助我得到解决方案.我没有得到这个异常的实际原因.

Vis*_*ngh 7

@EnableWebMvc在异常处理程序中缺少注释.您的异常处理程序应该是这样的.

@EnableWebMvc
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({ApplicationException.class})
    public void notFount(){
        System.out.println("----------CaughtApplicationException-----------");
    }

    @ExceptionHandler({Exception.class})
    public void notFountGlobal(){
        System.out.println("----------CaughtApplicationException-----------");
    }

}
Run Code Online (Sandbox Code Playgroud)

此外,您需要告诉异常处理程序方法您要在哪个方法中捕获哪个异常.最后,您可以指定一个方法,该方法可以捕获所有其他异常,并将其传递给Exception.class.您发布的代码段为我工作.希望它能解决你的问题.

  • `@ EnableWebMvc`是一个`@Configuration`注释,不会改变`@ControllerAdvice`的行为. (8认同)
  • 如果您正在创建Spring * Boot *应用程序,则不应使用@EnableWebMvc:https://dzone.com/articles/spring-boot-enablewebmvc-and-common-use-cases (4认同)

Sta*_*ley 6

在我的情况下,由于方法签名中的其他参数,未调用处理程序方法。

无法运作:

@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView handle404(HttpServletRequest request, Exception e,  @RequestHeader(value = "referer", required = false) final String referer) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

referer参数是什么原因造成的处理方法没有被调用。删除参数后问题解决。

作品:

@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView handle404(HttpServletRequest request, Exception e) {
    ...
}
Run Code Online (Sandbox Code Playgroud)