Spring Boot异常处理!!!

SpringBoot默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicErrorController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息

Spring Boot异常处理!!!_第1张图片

如 果我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再src/main/resources/

templates 目录下创建 error.html 页面。注意:名称必须叫 error

  1.controller:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 

Project: springboot - DomeContorller

*

Powered by scl On 2024-01-15 10:02:34

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @Controller public class DomeController { @RequestMapping("show1") public String show1() { System.out.println(6 / 0); return "error"; } @RequestMapping("show2") public String show2() { String str=null; str.length(); return "error"; } }

2.定义错误页面

Spring Boot异常处理!!!_第2张图片

error.html:




    
    Title


error1

出错啦!!!!汗流浃背了!!!

error.html:




    
    Title


error2

出错啦!!!!汗流浃背了!!!

3.定义全局异常处理类:

GlobalException:(普通的处理请求)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.exception;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 

Project: springboot - GlobalException

*

Powered by scl On 2024-01-15 10:11:29

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @Component public class GlobalException implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { ModelAndView modelAndView = new ModelAndView(); if (e instanceof NullPointerException){ modelAndView.setViewName("error1"); } else if (e instanceof ArithmeticException) { modelAndView.setViewName("error2"); } modelAndView.addObject("msg",e.toString()); return modelAndView; } }

AjaxGlobalException:(ajax的请求方式,返回的使json数据)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.exception;

import javafx.beans.property.adapter.ReadOnlyJavaBeanBooleanProperty;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 

Project: springboot - AjaxGlobalException

*

Powered by scl On 2024-01-15 10:48:17

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ //@ControllerAdvice public class AjaxGlobalException { @ResponseBody @ExceptionHandler public Map errorHandler(Exception e){ Map m = new HashMap<>(); m.put("status",500); m.put("msg",e.toString()); return m; } }

Spring Boot异常处理!!!_第3张图片

你可能感兴趣的:(spring,boot,java,spring)