全局捕獲異常:整個web請求項目全局捕獲異常java
應用場景:使用Aop技術,採用異常通知web
/** * *@author<a href="mailto:lei.tan@vtradex.net">譚磊</a> *@since2019-01-15 22:37 *全局捕獲異常案例 *1,捕獲返回json格式 *2,捕獲返回頁面 */ @ControllerAdvice(basePackages="com.example.comtroller") public class GlobalExceptionHandler { @ResponseBody//返回json格式 //modelAndView 返回頁面 @ExceptionHandler(RuntimeException.class)//攔截運行時異常 public Map<String, Object> errorJson(){
//實際開發中,怎麼將錯誤記錄在日誌中 Map<String, Object> map=new HashMap<String, Object>(); map.put("errorcode", "500"); map.put("errormessage", "系統錯誤"); return map; } }
@ExceptionHandler表示攔截異常spring
@ControllerAdivce是controller的一個輔助類,最經常使用的就是做爲全局異常處理json
@ControllerAdivce 能夠指定掃描範圍服務器
@ControllerAdivce 約定了幾種可行的返回值,若是是直接返回model類的話,須要app
@ResponseBody進行json轉換jsp
返回String,表示跳轉到某個view分佈式
返回modelAndViewspring-boot
返回model + @ResponseBodyspa
Log4j日誌管理
1.)
1. 新建log4配置文件(resources)
2.導入jar包
3.
@Controller public class JspController { private static final Logger logger =LoggerFactory.getLogger(JspController.class); @RequestMapping("/jspindex") public String index() { logger.info("404 集成log4j日誌錯誤"); return "index"; } }
2)Maven依賴
使用Aop統一處理Web請求日誌
<!-- Springboot 整合AOP --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
package com.example.aop; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.example.comtroller.JspController; /** * *@author<a href="mailto:lei.tan@vtradex.net">譚磊</a> *@since2019-01-22 21:24 */ @Aspect @Component public class WebLogAspect { private static final Logger logger =LoggerFactory.getLogger(JspController.class); @Pointcut("execution(public * com.example.controller.*.*(..))") public void weblog() { } /** * 使用Aop的前置通知 *tanlei *2019年1月22日 * */ @Before("weblog()") public void deBefore(JoinPoint joinPoint) throws Throwable{ //接收到請求,記錄請求內容 ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request=attributes.getRequest(); //記錄下請求內容 logger.info("URL:"+request.getRequestURI().toString()); logger.info("HTTP_METHOD:"+request.getMethod()); logger.info("IP:"+request.getRemoteAddr()); Enumeration<String> enu=request.getParameterNames(); while(enu.hasMoreElements()) { String name =(String)enu.nextElement(); logger.info("name:{},value:{}",name,request.getParameter(name)); } } //傳統寫在磁盤上有很大缺點,分佈式狀況 服務器集羣呢?20臺服務器,分佈式日誌收集 /** * 後置通知 *tanlei *2019年1月22日 * */ @AfterReturning(returning="ret",pointcut="weblog()") public void doAfterReturning(Object ret)throws Throwable { //處理完請求,返回內容 logger.info("Response:"+ret); } }