@ExceptionHandler的顺序 [英] Order of @ExceptionHandler

查看:1456
本文介绍了@ExceptionHandler的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用@ControllerAdvice来处理我所有的应用异常:

I use @ControllerAdvice to handle all my app exceptions :

@ControllerAdvice
public class ExceptionHandlingController {

  @ExceptionHandler({UnauthorizedException.class})
  public String unauthorizedException()  {
        .........
  }


  @ExceptionHandler({UnauthorizedAjaxException.class})
  @ResponseBody
  public void unauthorizedAjaxException()  {
        .........
  }

  @ExceptionHandler({Exception.class})
  public String globalException(){
        .........
  }


}

在我的代码中的某处我做throw new UnauthorizedException();

And somewhere in my code i do throw new UnauthorizedException();

   @Around("@annotation(Authenticated)")
   public Object profilingAuthentication(ProceedingJoinPoint pjp) throws Throwable  {

       HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

       if( request.getSession().getAttribute("idContact") == null ) {
            if( "XMLHttpRequest".equals(request.getHeader("X-Requested-With")) )
                throw new UnauthorizedAjaxException();
            throw new UnauthorizedException();
       } 
       return pjp.proceed();
   }

但是不幸的是,Spring MVC似乎是通过使用最普通的情况(Exception)而不是更具体的情况(例如UnauthorizedException)来随机执行的.有时他会选择正确的一个!

But sadly Spring MVC appears to be acting random by using the most generic case (Exception) rather than more specific ones (UnauthorizedException for example). And sometimes he choose the correct one !

订单如何运作?有什么方法可以指定顺序吗?

How the order works works ? and is there any way to specify the order ?

UnauthorizedException是自定义例外

public class UnauthorizedException extends Exception {

    public UnauthorizedException(){
        super();
    }

    public UnauthorizedException(String message){
        super(message);
    }
}

更新

我发现按非绝对的顺序实际上抛出UnauthorizedException的方法可以正常工作,而其他方法则不行!

i found out that the order it's not rondom actually the methods who throw UnauthorizedException works normally but the others not !

@Authenticated
@RequestMapping(value="/favoris") 
public String favoris(ModelMap model, HttpServletRequest request) 
      throws UnauthorizedException {
    ....
}

@Authenticated
@RequestMapping(value="/follow") 
public String follow(ModelMap model, HttpServletRequest request) {
    .....
}

所以我必须手动添加throws UnauthorizedException还是有其他解决方案?

So i have to add throws UnauthorizedException manually or there is some other solution ?

推荐答案

我们以以下方式使用异常处理程序,并且永不混乱,并且按预期运行. 因此,如果您将其用作以下示例,则有可能解决您的问题

we are using exception handler in following way and never order get mixed and it work as expected. So it could be possible if you will use it as following example then it will solve your problems

**********处理程序类别******************

**********handler class******************

@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = Exception.class)
    public boolean handle1(Exception exc) {
        System.out.println("#####Global Exception###" + exc);
        exc.printStackTrace(System.out);
        return true;
    }

    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = CustomException.class)
    public boolean handle2(CustomException exc) {
        System.out.println("###custom exception######" + exc);
        exc.printStackTrace(System.out);
        return true;
    }
}

***************控制器类************

***************Controller class************

@RestController("test")
@RequestMapping("/test1")
public class TestController {

    @RequestMapping("/t1")
    public boolean test() {
        if (true) {
            throw new CustomException();
        }
        return true;
    }
}

在上面的示例中,异常Habdler是 handle2 ,因为如果找不到该异常,它的第一个将搜索匹配的异常,然后寻找父级处理程序

In above example exception habdler is handle2 because 1st of all it will search for matching exception if not found then go for parrent handler

如果我们抛出新的NullPointerException(),它将搜索匹配的处理程序,但在这种情况下找不到,然后寻找 handle1

If we throw new NullPointerException() then it will search for matching handler but not found in this case then go for parrent that is handle1

有关更多信息,您可以在此处

for more you can refer here

我希望它能对您有所帮助.谢谢

I hope it will help you. Thanks

这篇关于@ExceptionHandler的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆