来自HandlerInterceptor

时间:2018-08-28 07:17:11

标签: java spring http spring-mvc spring-boot

我正在尝试创建一个HandlerIntecerptor,其预句柄具有如下代码结构

    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {

    boolean check = //do something
    if(!check)
    {
        throw new HttpStatusCodeException(HttpStatus.TOO_MANY_REQUESTS); //This line gives cannot be instantiated error.
    }
    return check;
}

但是它说类不能被实例化。有没有办法从preHandle内部抛出HTTP代码异常?

2 个答案:

答案 0 :(得分:2)

HttpStatusCodeException类是Abstract,不能实例化。

形成Javadoc

  

公共抽象类HttpStatusCodeException

使用HttpClientErrorExceptionHttpServerErrorException

答案 1 :(得分:0)

HttpStatusCodeException是一个抽象类,这意味着您必须先创建一个匿名内部类并首先覆盖抽象方法,然后才能抛出它。

检查此问题以获取其他信息Creating the instance of abstract class or anonymous class