zoukankan      html  css  js  c++  java
  • springboot 常用的异常处理方式

    springboot常用的异常处理推荐:

    一.创建一个异常控制器,并实现ErrorController接口:

    package com.example.demo.controller;
    
    import org.springframework.boot.web.servlet.error.ErrorController;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class BaseErrorController implements ErrorController {
    
    	@Override
    	public String getErrorPath() {
    		return "/error/error";
    	}
    	
    	@RequestMapping("/error")
    	public String getError() {
    		return getErrorPath();
    	}
    
    }
    

      当系统内发生错误后会跳转到error页面。

    二.创建一个异常句柄ErrorExceperHandler

    package com.example.demo.handler;
    
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseStatus;
    import org.springframework.web.servlet.ModelAndView;
    
    @ControllerAdvice
    public class ErrorExceperHandler {
    
    	@ExceptionHandler
    	@ResponseStatus(HttpStatus.OK)
    	public ModelAndView processException(Exception exception) {
    		ModelAndView modelAndView = new ModelAndView();
    		modelAndView.addObject("exception", exception.getMessage());
    		modelAndView.setViewName("/error/error");
    		return modelAndView;
    	}
    	
    	@ExceptionHandler
    	@ResponseStatus(HttpStatus.OK)
    	public ModelAndView processException(RuntimeException exception) {
    		ModelAndView modelAndView = new ModelAndView();
    		modelAndView.addObject("exception", exception.getMessage());
    		modelAndView.setViewName("/error/error");
    		return modelAndView;
    	}
    }
    

      重载方法针对Exception和RuntimeException进行拦截,当系统发生异常后,会跳转到异常页面。

    package com.example.demo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.example.demo.entity.Student;
    
    @Controller
    @RequestMapping("/student/")
    public class StudentController {
    	
    	@RequestMapping("show")
    	public String show(Model model) throws Exception {
    		
    		Student stu = new Student();
    		stu.setId(1001);
    		stu.setName("小明");
    		model.addAttribute("student", stu);
    		
    		if (!"a".equals("")) {
    			throw new RuntimeException("RuntimeException....");
    		}
    		
    		if (!"a".equals("")) {
    			throw new Exception("Exception....");
    		}
    		
    		return "show";
    	}
    
    }
    

      在做spring或springboot开发的时候推荐使用第二种。

  • 相关阅读:
    python3.4+pyspider爬58同城(二)
    pyspider安装后,点击run,报pyhton has stop working或python已停止运行的错误
    PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\Users\video\AppData\Local\Temp\tmpfipzk8ma'--问题解决
    使用firefoxprofile,selenium设置firefox,初始化firefox
    排序算法讲解
    Java寫聊天小程序
    csproj项目工程文件的脚本/动态链接库设置
    常见的内存加密防破解及安全方案
    Animator直接引用FBX下的AnimClip与直接引用单独的AnimClip的对比
    Jupyter多内核的手动配置(Python多版本)
  • 原文地址:https://www.cnblogs.com/blog411032/p/10364363.html
Copyright © 2011-2022 走看看