springboot(13)- 整合web开发(5)- 自定义异常

1 异常处理

1.1 静态页面

springboot(13)- 整合web开发(5)- 自定义异常_第1张图片

  • Controller
@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        int i = 1 / 0;
        return "hello";
    }
}

springboot(13)- 整合web开发(5)- 自定义异常_第2张图片

springboot(13)- 整合web开发(5)- 自定义异常_第3张图片

1.2 动态页面

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
 dependency>

springboot(13)- 整合web开发(5)- 自定义异常_第4张图片
springboot(13)- 整合web开发(5)- 自定义异常_第5张图片

1.3 异常源码分析

springboot(13)- 整合web开发(5)- 自定义异常_第6张图片
springboot(13)- 整合web开发(5)- 自定义异常_第7张图片

springboot(13)- 整合web开发(5)- 自定义异常_第8张图片

2 自定义异常数据

2.1 默认数据

springboot(13)- 整合web开发(5)- 自定义异常_第9张图片


<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<h1>templates-5xxh1>

<table border="1">
    <tr>
        <td>pathtd>
        <td th:text="${path}">td>
    tr>
    <tr>
        <td>timestamptd>
        <td th:text="${timestamp}">td>
    tr>
    <tr>
        <td>messagetd>
        <td th:text="${messages}">td>
    tr>
    <tr>
        <td>errortd>
        <td th:text="${error}">td>
    tr>
    <tr>
        <td>statustd>
        <td th:text="${status}">td>
    tr>

table>
body>
html>
  • Controller
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        int i = 1 / 0;
        return "hello";
    }
}

springboot(13)- 整合web开发(5)- 自定义异常_第10张图片

2.2 自定义异常数据

springboot(13)- 整合web开发(5)- 自定义异常_第11张图片

package com.tzb.exception;

import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

@Component
public class MyErrorAttr extends DefaultErrorAttributes {

    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace);
        map.put("myerror", "自定义的异常信息");
        
        return map;
    }
}

springboot(13)- 整合web开发(5)- 自定义异常_第12张图片
springboot(13)- 整合web开发(5)- 自定义异常_第13张图片

3 自定义异常视图

springboot(13)- 整合web开发(5)- 自定义异常_第14张图片

@Component
public class MyErrorViewResolver extends DefaultErrorViewResolver {
    /**
     * Create a new {@link DefaultErrorViewResolver} instance.
     *
     * @param applicationContext the source application context
     * @param resourceProperties resource properties
     */
    public MyErrorViewResolver(ApplicationContext applicationContext, ResourceProperties resourceProperties) {
        super(applicationContext, resourceProperties);
    }

    /**
     * 这里的 model 包含了默认的数据
     * @param request
     * @param status
     * @param model
     * @return
     */
    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("myerror");
        mv.addAllObjects(model);
        return mv;
    }
}

springboot(13)- 整合web开发(5)- 自定义异常_第15张图片


<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<h1>templates-myerror-5xxh1>

<table border="1">
    <tr>
        <td>pathtd>
        <td th:text="${path}">td>
    tr>
    <tr>
        <td>timestamptd>
        <td th:text="${timestamp}">td>
    tr>
    <tr>
        <td>messagetd>
        <td th:text="${messages}">td>
    tr>
    <tr>
        <td>errortd>
        <td th:text="${error}">td>
    tr>
    <tr>
        <td>statustd>
        <td th:text="${status}">td>
    tr>

    <tr>
        <td>myerrortd>
        <td th:text="${myerror}">td>
    tr>

table>
body>
html>

springboot(13)- 整合web开发(5)- 自定义异常_第16张图片

你可能感兴趣的:(#,13-2,Spring,Boot,springboot,自定义异常)