spring boot统一异常页面
只需要创建一个类就可以了
package com.ulic.gis.securityManage.controller; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.servlet.ModelAndView; @ControllerAdvice public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); @Autowired private ErrorAttributes errorAttributes; /** * 出现异常之后会跳转到此方法 * @param request * @param e * @return */ @ExceptionHandler(Exception.class) public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) { ModelAndView mav = new ModelAndView("/exception/blank"); Map<String, Object> map = getErrorAttributes(request, false); log.info("错误码status:{}",map.get("status")); //log.info("失败的请求:{}",map.get("URL")); mav.addObject("message","请求失败,存在异常数据"); return mav; } private Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) { ServletWebRequest servletWebRequest = new ServletWebRequest(request); Map<String, Object> map = this.errorAttributes.getErrorAttributes(servletWebRequest,includeStackTrace); String URL = request.getRequestURL().toString(); map.put("URL", URL); return map; } }
if you want to go fast,go alone,if you want to go far,go together