Home > Java > javaTutorial > body text

Three ways to handle Spring MVC exceptions uniformly

(*-*)浩
Release: 2019-08-29 16:22:01
forward
3440 people have browsed it

Spring has three methods of unified exception handling, namely:

Use @ExceptionHandler annotation, implement HandlerExceptionResolver interface, and use @controlleradvice annotation

Three ways to handle Spring MVC exceptions uniformly

Use @ExceptionHandler annotation

One disadvantage of using this annotation is that the method for exception handling must be in the same Controller as the method that caused the error. Use as follows:

@Controller      
public class GlobalController {
/**    
     * 用于处理异常的    
     * @return    
     */      
@ExceptionHandler({MyException.class})
public String exception(MyException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return "exception";
}
@RequestMapping("test")
public void test() {
throw new MyException("出错了!");
}
}
Copy after login

As you can see, the biggest flaw of this method is that it cannot globally control exceptions. Each class must be written once.

Implement the HandlerExceptionResolver interface

This method allows global exception control. For example:

@Component  
public class ExceptionTest implements HandlerExceptionResolver{
/**  
     * TODO 简单描述该方法的实现功能(可选).  
     * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)  
     */  
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,  
            Exception ex) {
System.out.println("This is exception handler method!");
return null;
}
}
Copy after login

Use @ControllerAdvice @ExceptionHandler annotation

As mentioned above @ExceptionHandler, the method that requires exception handling must be in the same Controller as the method that caused the error. Then when the code adds @ControllerAdvice, it does not need to be in the same controller. This is also a new feature brought by Spring 3.2. As can be seen from the name, it generally means controller enhancement. In other words, @controlleradvice @ExceptionHandler can also implement global exception catching.

Please ensure that this WebExceptionHandle class can be scanned and loaded into the Spring container.

@ControllerAdvice
@ResponseBody
public class WebExceptionHandle {
private static Logger logger = LoggerFactory.getLogger(WebExceptionHandle.class);
/**
     * 400 - Bad Request
     */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public ServiceResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
logger.error("参数解析失败", e);
return ServiceResponseHandle.failed("could_not_read_json");
}
/**
     * 405 - Method Not Allowed
     */
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ServiceResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
logger.error("不支持当前请求方法", e);
return ServiceResponseHandle.failed("request_method_not_supported");
}
/**
     * 415 - Unsupported Media Type
     */
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ServiceResponse handleHttpMediaTypeNotSupportedException(Exception e) {
logger.error("不支持当前媒体类型", e);
return ServiceResponseHandle.failed("content_type_not_supported");
}
/**
     * 500 - Internal Server Error
     */
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public ServiceResponse handleException(Exception e) {
if (e instanceof BusinessException){
return ServiceResponseHandle.failed("BUSINESS_ERROR", e.getMessage());
}
logger.error("服务运行异常", e);
e.printStackTrace();
return ServiceResponseHandle.failed("server_error");
}
}
Copy after login

If the exception type to be handled is not declared in the @ExceptionHandler annotation, it defaults to the exception type in the parameter list. So it can also be written like this:

@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler()
@ResponseBody
String handleException(Exception e){
return "Exception Deal! " + e.getMessage();
}
}
Copy after login

The parameter object is the exception object thrown by the Controller layer!

Inherit the ResponseEntityExceptionHandler class to implement global exception capture for the Rest interface, and can return a custom format:

@Slf4j
@ControllerAdvice
public class ExceptionHandlerBean  extends ResponseEntityExceptionHandler {
/**
     * 数据找不到异常
     * @param ex
     * @param request
     * @return
     * @throws IOException
     */
@ExceptionHandler({DataNotFoundException.class})
public ResponseEntity<Object> handleDataNotFoundException(RuntimeException ex, WebRequest request) throws IOException {
return getResponseEntity(ex,request,ReturnStatusCode.DataNotFoundException);
}
/**
     * 根据各种异常构建 ResponseEntity 实体. 服务于以上各种异常
     * @param ex
     * @param request
     * @param specificException
     * @return
     */
private ResponseEntity<Object> getResponseEntity(RuntimeException ex, WebRequest request, ReturnStatusCode specificException) {
ReturnTemplate returnTemplate = new ReturnTemplate();
returnTemplate.setStatusCode(specificException);
returnTemplate.setErrorMsg(ex.getMessage());
return handleExceptionInternal(ex, returnTemplate,
new HttpHeaders(), HttpStatus.OK, request);
}
}
Copy after login

The above is the detailed content of Three ways to handle Spring MVC exceptions uniformly. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!