We do web System time , All kinds of abnormalities are inevitable , But this kind of abnormality , Cannot be displayed directly on the client , So our server needs to do unified processing , Unified return format to client
It's used here spring Two notes of :@ControllerAdvice--- Controller enhancements and @ExceptionHandler --- exception handling
Let's take a look at the specific implementation , Create a class , add @ControllerAdvice annotation
/** * Unified exception handling class */ @ControllerAdvice public class MyExceptionHandler { private
static Logger logger = LoggerFactory.getLogger(MyExceptionHandler.class); /** *
Parameter parsing failure exception handling */ @ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class) @ResponseBody public
ResponseResult
handleHttpMessageNotReadableException(HttpMessageNotReadableException e,
HttpServletRequest request) { logger.error(request.getRequestURI() +
": Parameter parsing failed ",e); return new
ResponseResult(HttpStatus.BAD_REQUEST.value()," Parameter parsing failed "); } /** * Current request method exception handling is not supported
*/ @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ResponseBody
public ResponseResult
handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException
e, HttpServletRequest request) { logger.error(request.getRequestURI() +
": The current request method is not supported ",e); return new
ResponseResult(HttpStatus.METHOD_NOT_ALLOWED.value()," The current request method is not supported "); } /** *
Project operation exception handling */ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class) @ResponseBody public ResponseResult
handleException(Exception e, HttpServletRequest request) {
logger.error(request.getRequestURI() + ": Abnormal service operation ",e); return new
ResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value()," Abnormal service operation "); } /** *
Custom exception handling */ @ResponseStatus(HttpStatus.OK) @ExceptionHandler(MyException.class)
@ResponseBody public ResponseResult handleException(MyException e,
HttpServletRequest request) { logger.error(request.getRequestURI() +
": Custom inner exception ",e.getMsg()); return new ResponseResult(e.getCode(),e.getMsg()); } }
Here are just a few exceptions that we often encounter , Specific project operation exceptions can also be subdivided , Such as null pointer , Type conversion exception ,IO Abnormality, etc
The last one is custom exception handling
@Data public class MyException extends RuntimeException{ // Is it wrong private Integer
code; // error message private String msg; public MyException(Integer code, String msg) {
this.code = code; this.msg = msg; } }
Let's test it , Whether unified exception handling is effective
1. Inspection request type , We use get Request mode to test
2. Verify that the server is running abnormally , We intentionally write an exception in the code to test ( Should be thrown java.lang.ArithmeticException: / by zero)
3. Verify custom exceptions
summary : Test results , Exceptions can be handled uniformly
Technology
Daily Recommendation