简体   繁体   中英

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?

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;
}

You can do like this which will return a json response to the client

@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
    response.getWriter().write("{ \"error_description\": \"Invalid Value\"}");
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.setStatus(400);
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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