Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @无法调用ControllerAdvice异常处理程序方法_Java_Spring_Spring Mvc_Exception Handling_Controller - Fatal编程技术网

Java @无法调用ControllerAdvice异常处理程序方法

Java @无法调用ControllerAdvice异常处理程序方法,java,spring,spring-mvc,exception-handling,controller,Java,Spring,Spring Mvc,Exception Handling,Controller,我正在编写SpringMVC中异常处理的示例演示应用程序。我正在尝试使用@ControllerAdvice处理异常 我遵循了链接中描述的步骤 但是当我运行我的应用程序时,我得到了以下错误 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.test.core.ApplicationException 关于更多细节,以下是我正在学习的课程 A

我正在编写SpringMVC中异常处理的示例演示应用程序。我正在尝试使用@ControllerAdvice处理异常

我遵循了链接中描述的步骤

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

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.test.core.ApplicationException
关于更多细节,以下是我正在学习的课程

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
}
 @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
}  
}
@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler
public void notFount(){
    System.out.println("----------CaughtApplicationException-----------");
}
}
控制器类

ApplicationExceptionController.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
}
 @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
}  
}
@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler
public void notFount(){
    System.out.println("----------CaughtApplicationException-----------");
}
}
下面是GlobalExceptionHandler类

GlobalExceptionHandler.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
}
 @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
}  
}
@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler
public void notFount(){
    System.out.println("----------CaughtApplicationException-----------");
}
}
dispatcher 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>
我浏览了这个链接,但仍然遇到同样的问题
请帮助我获取解决方案。我无法获取此异常的实际原因。

您需要声明异常,以便将方法映射到异常类型。您有两种可能,一种是将异常声明为参数

public void notFount(ApplicationException exception){

将该类型添加为@ExceptionHandler注释的值属性

 @ExceptionHandler(value = ApplicationException.class)

此外,您很可能存在配置问题,因为使用您发布的erroneus
GlobalExceptionHandler
,servlet不会启动,明确表示没有异常类型映射到该方法。请确保您正在扫描
GlobalExceptionHandler
所在的包,从外观上看,这很可能是您的问题您在异常处理程序中缺少
@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-----------");
    }

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

在我的例子中,由于方法签名中有额外的参数,所以不会调用handler方法

不工作:

@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView handle404(HttpServletRequest request, Exception e,  @RequestHeader(value = "referer", required = false) final String referer) {
    ...
}
@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView handle404(HttpServletRequest request, Exception e) {
    ...
}
referer
参数是导致未调用处理程序方法的原因。删除该参数后,问题得到解决

有效:

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

我已经尝试过了,但仍然得到了相同的异常。控制器没有按照我在回答中提到的方式将您的异常映射到一个方法,并确保您扫描
com.test.core
包,其余的看起来很好,让我知道是否适合您正如您建议的那样,我检查了我的配置,我已经在我的配置中添加了
元素,还添加了
com.test.core
包。我已经更新了我的问题,请看一看。我希望通过更改配置它可以工作,请同时更改
GlobalExceptionHandler
,重新测试,并粘贴日志。我们更新了全局异常处理程序配置,在代码方面,您应该更好地回答这个问题,事实上,要扫描的数据包应该在
GlobalExceptionHandler
的任何位置,所以不是我在读取异常堆栈跟踪时错误地假设的
com.test.core
,相反,您的
GlobalExceptionHandler
类的包,无论它是否仍在工作。此代码是否在您这边工作?如果是,请共享您正在使用的jar名称,我认为缺少一些jarfile@rachana,SpringCore,SpringWeb,SpringWebMVC。你在使用maven project吗?不,我没有使用maven project。我在4.0.0版的项目中有所有这些JAR
@EnableWebMvc
是一个
@Configuration
注释,不会改变
@ControllerAdvice
的行为。如果你在创建Spring Boot应用程序,你不应该使用
@EnableWebMvc