简体   繁体   English

HandlerInterceptor抛出HTTP状态代码异常

[英]Throwing HTTP Status code exception from HandlerInterceptor

I am trying to create a HandlerIntecerptor whose pre-handle has the code structure as follows 我正在尝试创建一个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;
}

but it says class cannot be instantiated. 但是它说不能实例化类。 Is there a way to throw an http code exception from inside the preHandle? 有没有办法从preHandle内部抛出HTTP代码异常?

The class HttpStatusCodeException is Abstract and can not be instanciated. HttpStatusCodeException类是Abstract,不能实例化。

Form the Javadoc : 形成Javadoc

public abstract class HttpStatusCodeException 公共抽象类HttpStatusCodeException

Use HttpClientErrorException or HttpServerErrorException 使用HttpClientErrorExceptionHttpServerErrorException

HttpStatusCodeException is an abstract class which means that you cannot throw it without creating an anonymous inner class and overriding the abstract method first. HttpStatusCodeException是一个抽象类,这意味着您必须先创建一个匿名内部类并首先覆盖该抽象方法,然后才能抛出它。

Check this question for additional info Creating the instance of abstract class or anonymous class 检查此问题以获取其他信息创建抽象类或匿名类的实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM