3

I am trying to create a HandlerIntecerptor whose pre-handle has the code structure as follows

    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?

Dhrubojit
  • 93
  • 1
  • 1
  • 5

2 Answers2

13

The class HttpStatusCodeException is Abstract and can not be instanciated.

Form the Javadoc:

public abstract class HttpStatusCodeException

Use HttpClientErrorException or HttpServerErrorException

Jens
  • 67,715
  • 15
  • 98
  • 113
-1

HttpStatusCodeException is an abstract class which means that you cannot throw it without creating an anonymous inner class and overriding the abstract method first.

Check this question for additional info Creating the instance of abstract class or anonymous class

faris
  • 692
  • 4
  • 18