Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send response from an interceptor, and halt further execution?

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?

like image 710
Blankman Avatar asked Dec 29 '12 23:12

Blankman


People also ask

How do you intercept a response in spring boot?

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.

Which of the below methods that each handler interceptor must implement?

Each handler interceptor must implement the HandlerInterceptor interface, which contains three callback methods for you to implement: preHandle() , postHandle() , and afterCompletion() .

Which interceptor is used in an application when maintenance page needs to be shown?

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.

What is HandlerInterceptorAdapter in spring?

HandlerInterceptorAdapter is abstract adapter class for the HandlerInterceptor interface, for simplified implementation of pre-only/post-only interceptors.


1 Answers

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;
}
like image 120
Kevin Bowersox Avatar answered Sep 20 '22 15:09

Kevin Bowersox