1 describe

Exceptions in the system include compile-time exceptions and runtimeexceptions. The former captures exceptions to obtain exception information, while the latter reduces the occurrence of run-time exceptions by means of standard code development and testing. In the development of J2EE project, it is inevitable to encounter all kinds of predictable and unpredictable anomalies to deal with, no matter for the bottom database operation process, or the processing process of the business layer, or the processing process of the control layer. Each process deals with the exception separately, the system’s code coupling degree is high, the workload is large and not unified, and the workload of maintenance is also large.

Then, can all types of exception processing be decoupled from each processing process, so as to ensure that the function of the related processing process is relatively single, and realize the unified processing and maintenance of exception information? The answer is yes. The following describes the resolution and implementation of unified exception handling using Spring MVC.

2 ideas

We throw all exceptions of the DAO, Service, and controller of the system upward through throws Exception, and finally the SpringMVC front-end controller submits them to the Exception processor for Exception processing. Springmvc provides a global exception handler (one exception handler per system) for uniform exception handling. Now that you understand the exception handling mechanism in SpringMVC, let’s use Spring annotations to implement exception handling.

3 of actual combat

3.1 servce layer

@Service
public class OrganizationVoServiceImpl implements OrganizationVoService {
 
	@Autowired
	private OrganizationVoMapper organizationVoDao;
 
	// Query all records
	public MyResult findAllRecords(OrganizationVo record) throws Exception{
			List<OrganizationVo> list = organizationVoDao.findAllList(record);
			int count = (int) organizationVoDao.findAllListCount(record);
			throw new NullPointerException();
// return MyResult.ok(count, list);}}Copy the code

We define a null-pointer exception in the findAllRecords method of the Service for testing. All exceptions in the Service are thrown upward with throws

 

3.2 the controller layer

@Controller
@RequestMapping("organizationVoController")
public class OrganizationVoController extends BaseController{
 
 
	@Autowired
	private  OrganizationVoService  organizationVoService;
 
	// Query all records
	@RequestMapping("findAllRecords")
	@ResponseBody
	public MyResult  findAllRecords(OrganizationVo record) throws Exception{
		MyResult result=organizationVoService.findAllRecords(record);
		returnresult; }}Copy the code

3.3 Controller Exception Notification (Key)

@ControllerAdvice
public class AppWideExceptionHandler {
	
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public MyResult exception(HttpServletRequest request, Exception ex){
		String msg="Exception information >>>>> Exception name :"+ex.getClass()+"| | method name:"+ex.getStackTrace()[0].getMethodName()+"| | the name of the class:"+ex.getStackTrace()[0].getClassName()+"| | lines."+ex.getStackTrace()[0].getLineNumber();
		returnMyResult.error(msg); }}Copy the code

@ExceptionHandler: When an Exception is thrown, this method is delegated to handle it. The custom Exception class can be filled in parentheses

@controlleradvice: ControllerAdvice is any class with @controlleradvice. This class contains one or more methods of the following types:

@ExceptionHandler Method for annotation annotation

InintBinder Annotation method

@modelAttribute Annotation method

In a class annotated with @ControllerAdvice, the methods described above will apply to methods annotated with @RequestMapping on all controllers throughout the application.

The Exception parameter in the method retrieves Exception information

Here are the results