• spring boot ---web应用开发-错误处理


    一.错误的处理

    方法一:Spring Boot 将所有的错误默认映射到/error实现ErrorController

    @Controller
    @RequestMapping(value = "error")
    public class BaseErrorController implements ErrorController {
    private static final Logger logger = LoggerFactory.getLogger(BaseErrorController.class);
    
    	@Override
    	public String getErrorPath() {
    		logger.info("出错啦!进入自定义错误控制器");
    		return "error/error";
    	}
    
    	@RequestMapping
    	public String error() {
    		return getErrorPath();
    	}
    
    }

    方法二:添加自定义的错误页面

    2.1 html静态页面:在resources/public/error/ 下定义

    如添加404页面: resources/public/error/404.html页面,中文注意页面编码

    2.2 模板引擎页面:在templates/error/下定义

    如添加5xx页面: templates/error/5xx.ftl

     

    注:templates/error/ 这个的优先级比较高 resources/public/error/

    方法三:使用注解@ControllerAdvice

     

    package com.demo.example.handler;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    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 ErrorExceptionHandler{
    	private static final Logger logger = LoggerFactory.getLogger(ErrorExceptionHandler.class);
    	
    	@ExceptionHandler({RuntimeException.class})
    	@ResponseStatus(HttpStatus.OK)
    	public ModelAndView processException(RuntimeException exception) {
    		logger.info("自定义异常-RuntimeException");
    		ModelAndView modelAndView = new ModelAndView();
    		modelAndView.addObject("roncooException", exception.getMessage());
    		modelAndView.setViewName("error/500");
    		return modelAndView;
    	}
    	
    	@ExceptionHandler({Exception.class})
    	@ResponseStatus(HttpStatus.OK)
    	public ModelAndView processException(Exception exception) {
    		ModelAndView modelAndView = new ModelAndView();
    		logger.info("自定义异常-Exception");
    		modelAndView.addObject("roncooException", exception.getMessage());
    		modelAndView.setViewName("error/500");
    		return modelAndView;
    	}
    	
    	
    }
    

     

      

     

    一.错误的处理

    方法一:Spring Boot 将所有的错误默认映射到/error实现ErrorController

    @Controller

    @RequestMapping(value = "error")

    public class BaseErrorController implements ErrorController {

    private static final Logger logger = LoggerFactory.getLogger(BaseErrorController.class);

     

    @Override

    public String getErrorPath() {

         logger.info("出错啦!进入自定义错误控制器");

         return "error/error";

    }

     

    @RequestMapping

    public String error() {

         return getErrorPath();

    }

     

    }

     

    方法二:添加自定义的错误页面

    2.1 html静态页面:在resources/public/error/ 下定义

    如添加404页面: resources/public/error/404.html页面,中文注意页面编码

    2.2 模板引擎页面:在templates/error/下定义

    如添加5xx页面: templates/error/5xx.ftl

    注:templates/error/ 这个的优先级比较 resources/public/error/

     

    方法三:使用注解@ControllerAdvice

    /**

     * Copyright 2015-2016 广州市领课网络科技有限公司

     */

    package com.demo.example.handler;

     

    import org.slf4j.Logger;

    import org.slf4j.LoggerFactory;

    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;

     

    /**

     * 异常处理类

     *

     * @author hugovon

     * @version 1.0

     */

    @ControllerAdvice

    publicclass ErrorExceptionHandler{

        privatestaticfinal Logger logger = LoggerFactory.getLogger(ErrorExceptionHandler.class);

       

        @ExceptionHandler({RuntimeException.class})

        @ResponseStatus(HttpStatus.OK)

        public ModelAndView processException(RuntimeException exception) {

            logger.info("自定义异常-RuntimeException");

            ModelAndView modelAndView = new ModelAndView();

            modelAndView.addObject("roncooException", exception.getMessage());

            modelAndView.setViewName("error/500");

            returnmodelAndView;

        }

       

        @ExceptionHandler({Exception.class})

        @ResponseStatus(HttpStatus.OK)

        public ModelAndView processException(Exception exception) {

            ModelAndView modelAndView = new ModelAndView();

            logger.info("自定义异常-Exception");

            modelAndView.addObject("roncooException", exception.getMessage());

            modelAndView.setViewName("error/500");

            returnmodelAndView;

        }

       

       

    }

  • 相关阅读:
    部署ArcGIS JS API 离线包(Tomcat与IIS)
    BootStrap入门教程 (一)
    找个些有用的网站(CSS生成)
    android 调用系统相机拍照 获取原图
    Android 拍照
    Android调用Webservice发送文件
    Android 开发添加控件事件的三种方式
    vim中的ctrl+s导致的“假死”、无响应、不接受输入
    Ubuntu下eclipse中运行Hadoop时所需要的JRE与JDK的搭配
    ubuntu下安装bin文件
  • 原文地址:https://www.cnblogs.com/durenniu/p/9520189.html
Copyright © 2020-2023  润新知