I have an interceptor, and under a certain condition I want to send a string response to the browser and then halt execution completely.
How can I do this?
To work with interceptor, you need to create @Component class that supports it and it should implement the HandlerInterceptor interface. preHandle() method − This is used to perform operations before sending the request to the controller. This method should return true to return the response to the client.
Each handler interceptor must implement the HandlerInterceptor interface, which contains three callback methods for you to implement: preHandle() , postHandle() , and afterCompletion() .
2. MaintenanceInterceptor. Intercept before the controller execution, check if the current time is in between the maintenance time, if yes then redirect it to maintenance page; else continue the execution chain.
HandlerInterceptorAdapter is abstract adapter class for the HandlerInterceptor interface, for simplified implementation of pre-only/post-only interceptors.
Override the preHandle method and return false if you want to stop execution.
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
response.getWriter().write("something");
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With