// 全局异常拦截(拦截项目中的NotLoginException异常) @ExceptionHandler(NotLoginException.class) public void handlerNotLoginException(NotLoginException nle, HttpServletRequest request, HttpServletResponse response) throws Exception { //错误信息 String message "错误"; // 返回给前端 String jsonString = "{\"success\":false,\"code\":404,\"message\":\""+message+"\"}"; response.setCharacterEncoding("utf-8"); response.setStatus(404); response.setContentType("application/json; charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write(jsonString); }