javaweb項目自定義HTTP請求Response返回JSON數據


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM