Chapter 2 springboot + global exception handling and springboot Exception Handling

Source: Internet
Author: User

Chapter 2 springboot + global exception handling and springboot Exception Handling

1. Exception Handling within a single controller

1 package com. xxx. secondboot. web; 2 3 import org. springframework. web. bind. annotation. exceptionHandler; 4 import org. springframework. web. bind. annotation. requestMapping; 5 import org. springframework. web. bind. annotation. requestMethod; 6 import org. springframework. web. bind. annotation. restController; 7 8 import com. xxx. secondboot. exception. myExceptionResponse; 9 10 import io. swagger. annotations. api; 11 12 @ Api ("test controllerAdvice and global exception handling") 13 @ RestController14 @ RequestMapping ("/advice1 ") 15 public class AdviceController {16 17 @ RequestMapping (value = "/test1", method = RequestMethod. GET) 18 public String test1 () {19 throw new RuntimeException ("advice1-exception1"); 20} 21 22 @ RequestMapping (value = "/test2", method = RequestMethod. GET) 23 public String test2 () {24 throw new RuntimeException ("advice1-exception2"); 25} 26 27 @ ExceptionHandler (RuntimeException. class) 28 public MyExceptionResponse exceptionHandler () {29 MyExceptionResponse resp = new MyExceptionResponse (); 30 resp. setCode (300); 31 resp. setMsg ("exception-Handler"); 32 return resp; 33} 34 35}

Note:

  • Add the class modified by @ ExceptionHandler to the controller (specify the exception classes to be processed by the method in the annotation)
  • The exception handling method only works in the current controller.

Ii. Exception Handling for All controllers (Global Exception Handling)

1. Global Exception Handling

1 package com. xxx. secondboot. web; 2 3 import javax. servlet. http. httpServletResponse; 4 5 import org. springframework. web. bind. annotation. controllerAdvice; 6 import org. springframework. web. bind. annotation. exceptionHandler; 7 import org. springframework. web. bind. annotation. responseBody; 8 import org. springframework. web. bind. annotation. restController; 9 10 import com. xxx. secondboot. exception. myExceptionResponse; 11 import com. xxx. secondboot. exception. myRuntimeException; 12 13 // @ ControllerAdvice (annotations = RestController. class) 14 // @ ControllerAdvice (basePackages = {"com. xxx "," com. ooo "}) 15 @ ControllerAdvice16 public class GlobalExceptionHandler {17 @ ExceptionHandler (RuntimeException. class) 18 // @ ExceptionHandler (value = {RuntimeException. class, MyRuntimeException. class}) 19 // @ ExceptionHandler // handle all exceptions 20 @ ResponseBody // This must be included when a custom class is returned, this is the description of @ ControllerAdvice annotation 21 public MyExceptionResponse exceptionHandler (RuntimeException e, HttpServletResponse response) {22 MyExceptionResponse resp = new MyExceptionResponse (); 23 resp. setCode (300); 24 resp. setMsg ("exception-Handler"); 25 // response. setStatus (600); 26 return resp; 27} 28}

Note:

  • @ ControllerAdvice is a helper class of the controller. The most common class is the partition class used for global exception handling.
  • @ ControllerAdvice: You can specify the scan range.
  • @ ControllerAdvice specifies several feasible return values. If a model class is returned directly, @ ResponseBody must be used for json conversion.
    • Returns a String to jump to a view.
    • Return modelAndView
    • Return model + @ ResponseBody

2. controller

1 package com. xxx. secondboot. web; 2 3 import org. springframework. web. bind. annotation. requestMapping; 4 import org. springframework. web. bind. annotation. requestMethod; 5 import org. springframework. web. bind. annotation. restController; 6 7 import io. swagger. annotations. api; 8 9 @ Api ("test controllerAdvice and global exception handling") 10 @ RestController11 @ RequestMapping ("/advice1 ") 12 public class AdviceController {13 14 @ RequestMapping (value = "/test1", method = RequestMethod. GET) 15 public String test1 () {16 throw new RuntimeException ("advice1-exception1"); 17} 18 19 @ RequestMapping (value = "/test2", method = RequestMethod. GET) 20 public String test2 () {21 throw new RuntimeException ("advice1-exception2"); 22} 23 24 // @ ExceptionHandler (RuntimeException. class) 25 // public MyExceptionResponse exceptionHandler () {26 // MyExceptionResponse resp = new MyExceptionResponse (); 27 // resp. setCode (300); 28 // resp. setMsg ("exception-Handler"); 29 // return resp; 30 //} 31 32}

Note:

  • If the same exception is overwritten by both a local range exception processor and a global range exception processor, a small range of local range processor will be selected.
  • If a single exception is overwritten by a small range of exception classes and a large range of exception processors, a small range of exception processors will be selected.

Reference:

  • Http://jinnianshilongnian.iteye.com/blog/1866350 Kai Tao @ ControllerAdvice (three roles)
  • Exception Handling System defined by http://www.tuicool.com/articles/fA7nuii springboot
  • SpringMVC Exception Handling System for https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
  • SpringMVC Exception Handling System for http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.