1. 程式人生 > >spring 全域性異常處理類

spring 全域性異常處理類

spring全域性異常處理
@ControllerAdvice 註解包含@Component註解,在context:component-scan掃描包的時候,注意掃描方式,需要把異常處理類掃描進去

package com.admin.exceptioncontroller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import
org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import
org.springframework.web.bind.annotation.RestController; import org.springframework.stereotype.Controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.NoHandlerFoundException; import com.admin.controller.BaseController; import com.admin.enumdata.ServerErrorEnum; import
com.admin.utils.CommonResponse; import com.admin.utils.JsonUtils; @ControllerAdvice(annotations = { RestController.class, Controller.class })// public class ExceptionReaper extends BaseController{ private static final Logger logger = LoggerFactory.getLogger(ExceptionReaper.class); @ExceptionHandler(value = { IOException.class, RuntimeException.class }) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ModelAndView exception(Exception exception, HttpServletRequest request, HttpServletResponse response) { logger.error(ServerErrorEnum.INTERNAL_SERVER_ERROR.getMessage(), exception); if(jsAjaxRequest(request)){ writeResponse(response, bad(ServerErrorEnum.INTERNAL_SERVER_ERROR.getMessage())); return null; } return new ModelAndView(ServerErrorEnum.INTERNAL_SERVER_ERROR.getErrPage()); } @ExceptionHandler(value = { NoHandlerFoundException.class }) @ResponseStatus(HttpStatus.NOT_FOUND) public ModelAndView noMapping(Exception exception, HttpServletRequest request, HttpServletResponse response) { logger.error(ServerErrorEnum.ACCESS_NOT_FOUND.getMessage(), exception); if(jsAjaxRequest(request)){ writeResponse(response, bad(ServerErrorEnum.ACCESS_NOT_FOUND.getMessage())); return null; } return new ModelAndView(ServerErrorEnum.ACCESS_NOT_FOUND.getErrPage()); } //是否是ajax請求 private boolean jsAjaxRequest(HttpServletRequest request) { boolean isAjaxRequest = false; //如果含有"application/json" 或者含有"XMLHttpRequest" 表明是ajax請求 if (request.getHeader("accept").contains("application/json") || (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").contains("XMLHttpRequest"))) { isAjaxRequest = true; } return isAjaxRequest; } //response 寫入返回資訊 private void writeResponse(HttpServletResponse response, CommonResponse cRes){ try { response.setCharacterEncoding("UTF-8"); PrintWriter writer = response.getWriter(); writer.write(JsonUtils.toJson(cRes)); writer.flush();//因為寫入的時候是有快取的,快取夠了才會寫入,這塊是將快取中的資料強制寫入 writer.close();//呼叫close預設呼叫flush,一般servlet或者spring等框架在response返回的時候,會自動呼叫close方法,所以不需要顯示呼叫flush和close } catch (IOException e) { logger.error("spring全域性異常捕獲類:writer response 異常"); } } } package com.admin.enumdata; public enum ServerErrorEnum { INTERNAL_SERVER_ERROR("error/internal_server_error", "spring全域性異常捕獲類:伺服器500異常"), ACCESS_NOT_FOUND("error/not_found", "spring全域性異常捕獲類:伺服器404異常"); private ServerErrorEnum(String errPage, String msg) { this.errPage = errPage; this.message = msg; } private String errPage; private String message; public String getErrPage() { return errPage; } public void setErrPage(String errPage) { this.errPage = errPage; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }