这篇文章给大家分享的是有关Spring MVC异常统一处理有几种方式的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
,春天统一异常处理有三种方式,分别为:
使用@ ExceptionHandler注解,实现HandlerExceptionResolver接口,使用@controlleradvice注解
<强>使用@ ExceptionHandler注解强>
使用该注解有一个不好的地方就是:进行异常处理的方法必须与出错的方法在同一个控制器里面。使用如下:
@ Controller 公开课GlobalController {/* * *用于处理异常的 * @return */@ExceptionHandler ({MyException.class}) 公共字符串异常(MyException e) { System.out.println (e.getMessage ()); e.printStackTrace (); 返回“exception"; } @RequestMapping (“test") 公共空白测试(){ 把新的MyException(“出错了!“); } }
可以看的到,这种方式最大的缺陷就是不能全局控制异常。每个类都要写一遍。
实现HandlerExceptionResolver接口
这种方式可以进行全局的异常控制,例如:
@ component 公共类ExceptionTest实现HandlerExceptionResolver {/* * * TODO简单描述该方法的实现功能(可选)。 * @see org.springframework.web.servlet.HandlerExceptionResolver # resolveException (javax.servlet.http。HttpServletRequest javax.servlet.http。HttpServletResponse . lang。对象,java.lang.Exception) */公共ModelAndView resolveException (HttpServletRequest请求,HttpServletResponse响应对象处理程序, 异常的前女友){ System.out.println(“这是异常处理程序方法!“); 返回null; } }
<强>使用@ControllerAdvice + @ ExceptionHandler注解强>
上文说到@ ExceptionHandler需要进行异常处理的方法必须与出错的方法在同一个控制器里面。那么当代码加入了@ControllerAdvice,则不需要必须在同一个控制器中了。这也是Spring 3.2带来的新特性。从名字上可以看出大体意思是控制器增强。也就是说,@ControllerAdvice + @ ExceptionHandler也可以实现全局的异常捕捉。
请确保此WebExceptionHandle类能被扫描到并装载进春容器中。
@ControllerAdvice @ResponseBody 公开课WebExceptionHandle { 私有静态日志记录器=LoggerFactory.getLogger (WebExceptionHandle.class);/* * * 400 -错误请求 */@ResponseStatus (HttpStatus.BAD_REQUEST) @ExceptionHandler (HttpMessageNotReadableException.class) 公共ServiceResponse handleHttpMessageNotReadableException (HttpMessageNotReadableException e) { logger.error(“参数解析失败“,e); 返回ServiceResponseHandle.failed (“could_not_read_json"); }/* * * 405 -方法不允许的 */@ResponseStatus (HttpStatus.METHOD_NOT_ALLOWED) @ExceptionHandler (HttpRequestMethodNotSupportedException.class) 公共ServiceResponse handleHttpRequestMethodNotSupportedException (HttpRequestMethodNotSupportedException e) { logger.error(“不支持当前请求方法“,e); 返回ServiceResponseHandle.failed (“request_method_not_supported"); }/* * * 415 -不支持的媒体类型 */@ResponseStatus (HttpStatus.UNSUPPORTED_MEDIA_TYPE) @ExceptionHandler (HttpMediaTypeNotSupportedException.class) 公共ServiceResponse handleHttpMediaTypeNotSupportedException(异常e) { logger.error(“不支持当前媒体类型“,e); 返回ServiceResponseHandle.failed (“content_type_not_supported"); }/* * * 500 -内部服务器错误 */@ResponseStatus (HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler (Exception.class) 公共ServiceResponse handleException(异常e) { 如果(e instanceof BusinessException) { 返回ServiceResponseHandle.failed (“BUSINESS_ERROR" e.getMessage ()); } logger.error(“服务运行异常“,e); e.printStackTrace (); 返回ServiceResponseHandle.failed (“server_error"); } }
如果@ExceptionHandler注解中未声明要处理的异常类型,则默认为参数列表中的异常类型,所以还可以写成这样:
@ControllerAdvice 公开课GlobalExceptionHandler { @ExceptionHandler () @ResponseBody 字符串handleException(异常e) { 返回“异常交易!“;+ e.getMessage (); } }
参数对象就是控制器层抛出的异常对象!
继承ResponseEntityExceptionHandler类来实现针对其他接口的全局异常捕获,并且可以返回自定义格式:
@Slf4j @ControllerAdvice 公开课ExceptionHandlerBean延伸ResponseEntityExceptionHandler {/* * *数据找不到异常 * @param交货 * @param请求 * @return * @throws IOException */@ExceptionHandler ({DataNotFoundException.class}) 公共ResponseEntitySpring MVC异常统一处理有几种方式