我有一个拦截器,在某种情况下我想向浏览器发送一个字符串响应,然后完全停止执行。
我该怎么做?
答案 0 :(得分:13)
覆盖preHandle方法,如果要停止执行,则返回false。
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
response.getWriter().write("something");
return false;
}
答案 1 :(得分:3)
您可以这样做,它将向客户端返回json响应
@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;
}