Use @ControllerAdvice+ @ ExceptionHandler annotations
Reference materials
Body p>
Spring unified exception handling has three methods, namely:
Using @ ExceptionHandler annotation
Implementing the HandlerExceptionResolver interface
Using @ Controlleradvice annotation
Using @ ExceptionHandler annotation
There is a downside to using this annotation: exception handling The method must be in the same Controller as the error method. Use it as follows:
1 @Controller
2 public class GlobalController {
3
4 /**
5 * Used to handle exceptions
6 * @return
7 */
8 @ExceptionHandler({MyException.class})
9 public String exception(MyException e) {
10 System.out.println(e.getMessage());
11 e.printStackTrace();
12 return "exception";
13}
14
15 @RequestMapping("test")
16 public void test() {
17 throw new MyException("Something went wrong!");
18}
19}
It can be seen that the biggest drawback of this method is that it cannot control the exception globally. Each class must be written again.
Back to the top
Implement the HandlerExceptionResolver interface
This method allows global exception control. For example:
1 @Component
2 public class ExceptionTest implements HandlerExceptionResolver{
3
4 /**
5 * TODO briefly describes the realization function of this method (optional).
6 * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
7 */
8 public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
9 Exception ex) {
10 System.out.println("This is exception handler method!");
11 return null;
12}
13}
Back to top
Use @ControllerAdvice+ @ ExceptionHandler annotation
As mentioned above, @ ExceptionHandler needs to perform exception handling methods must be in the same Controller as the error method. Then when the code is added @ControllerAdvice, it does not need to be in the same controller. This is also a new feature brought by Spring 3.2. It can be seen from the name that it basically means controller enhancement. In other words, @controlleradvice + @ ExceptionHandler can also achieve global exception capture.
Please make sure that this WebExceptionHandle class can be scanned and loaded into the Spring container.
1 @ControllerAdvice
2 @ResponseBody
3 public class WebExceptionHandle {
4 private static Logger logger = LoggerFactory.getLogger(WebExceptionHandle.class);
5 /**
6 * 400-Bad Request
7 */
8 @ResponseStatus(HttpStatus.BAD_REQUEST)
9 @ExceptionHandler(HttpMessageNotReadableException.class)
10 public ServiceResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
11 logger.error("Parameter parsing failed", e);
12 return ServiceResponseHandle.failed("could_not_read_json");
13}
14
15 /**
16 * 405-Method Not Allowed
17 */
18 @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
19 @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
20 public ServiceResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
21 logger.error("The current request method is not supported", e);
22 return ServiceResponseHandle.failed("request_method_not_supported");
23}
24
25 /**
26 * 415-Unsupported Media Type
27 */
28 @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
29 @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
30 public ServiceResponse handleHttpMediaTypeNotSupportedException(Exception e) {
31 logger.error("The current media type is not supported", e);
32 return ServiceResponseHandle.failed("content_type_not_supported");
33}
34
35 /**
36 * 500-Internal Server Error
37 */
38 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
39 @ExceptionHandler(Exception.class)
40 public ServiceResponse handleException(Exception e) {
41 if (e instanceof BusinessException){
42 return ServiceResponseHandle.failed("BUSINESS_ERROR", e.getMessage());
43}
44
45 logger.error("Service Operation Abnormal", e);
46 e.printStackTrace();
47 return ServiceResponseHandle.failed("server_error");
48}
49 }
If the exception type to be handled is not declared in the @ExceptionHandler annotation, it will default to the exception type in the parameter list. So it can also be written like this:
@ControllerAdvice
public class GlobalExceptionHandler {