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