// 全局異常攔截(攔截項目中的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); }