java錯誤處理
阿新 • • 發佈:2018-11-11
監聽器,獲取使用者裝置資訊
package com.xingxue.Interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Enumeration; public class InterceptorA implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Enumeration<String> names = request.getHeaderNames(); while(names.hasMoreElements()){ String n=names.nextElement(); String v=request.getHeader(n); System.out.println(n+" "+v); } System.out.println("攔截前+++"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("攔截後+++"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("最終+++"); } }
題解
異常處理
package com.xingxue.springmvc; import com.xingxue.domain.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/demo") public class DemoController { @RequestMapping("/index") public String index(){ return "demo"; } @RequestMapping("/login") public String login2(){ User u=new User(); u=null; u.toString(); return "login"; } @ExceptionHandler(Exception.class) public String error(Exception e){ System.out.println(e); return "error"; } }
在實際開發過程中,我們通常會把應用後端分為3層來開發,Controller(控制器) Service(服務層) Dao(資料訪問層),這3層都可能會有異常的發生,通常的做法是把異常,由 DAO 拋到 Service 再拋到 Controller,最後再控制其中處理異常。這樣可以把異常反饋給最頂層呼叫者處理。但是一個系統中有若干個Controller ,若干個方法,處理異常就變得十分難受了,Springmvc提供了一種全域性異常處理機制,允許我們在一個類中集中處理異常,控制器不需理會異常。
實現異常處理方式一:
@ExceptionHandler({IOException.class,NullPointerException.class } ) public String excpetionHandle(){ return "error"; }
註解中的異常型別可以給 單個 或 多個
這種處理方式,比較侷限,只能處理當前 Controller中的異常
實現全域性異常處理方式一:
①實現 HandlerExceptionResolver 介面
//確保 AppGolablException 例項加入IOC容器中 可以用xml配置和註解配置 @Component public class AppGolablException implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { //這裡無論什麼異常都到 error 頁面去 //當然這裡可以根據實際的異常型別做 不同的響應 ModelAndView mv = new ModelAndView("error"); return mv; } }
②丟擲異常
@RequestMapping("/ex") public String testex(){ throw new NullPointerException("hehe"); } @RequestMapping("/ex2") public String textex2() throws Exception{ throw new IOException("haha"); }
這時請求 ex 和 ex2 方法都會跳轉到 error頁面
實現全域性異常處理方式二: @ControllerAdvice + @ExceptionHandler
①編寫異常處理程式碼
@ControllerAdvice public class AppException { @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception.class) @ResponseBody public Object handleHttpMessageNotReadableException(Exception e) { System.out.println("------------"); return "50X"; } }
②控制器丟擲異常
@RequestMapping("/ex") public String testex(){ throw new NullPointerException("hehe"); } @RequestMapping("/ex2") public String textex2() throws Exception{ throw new IOException("haha"); }