SpringBoot thymeleaf自定义错误页面

简介: SpringBoot thymeleaf自定义错误页面

导入thymeleaf

  • pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

自定义异常类

  • 建立监听异常类

MyException.class

package com.example.demo.domain;
public class MyException extends RuntimeException {
    private int code;
    private String msg;
    public MyException(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

CustomExtHandle 监测异常

package com.example.demo.domain;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
@RestControllerAdvice
public class CustomExtHandle {
    // 捕获全局异常
    @ExceptionHandler(value = Exception.class)
    Object handleException(Exception e, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", e.getMessage());
        map.put("url", request.getRequestURL());
        return map;
    }
    // 如果是Myexception类
    @ExceptionHandler(value = MyException.class)
    Object handleMyException(MyException e, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error.html"); // 指定错误跳转页面 需要在templates里面新建 一个error.html
        modelAndView.addObject("msg", e.getMsg());
        modelAndView.addObject("code", e.getCode());
        modelAndView.addObject("url", request.getRequestURL());
        return modelAndView;
        // 当然这里也可以返回json数据 前后台分离的话直接返回一个json即可
    }
}

template/error.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>出异常了</h1>
<span>错误信息:</span><h1 th:text="${msg}"></h1>    // 获取变量
<span>错误状态码:</span><h1 th:text="${code}"></h1>
<span>失败API地址:</span><h1 th:text="${url}"></h1>
</body>
</html>

使用

@RequestMapping("/user_info")
    public Map<String, String> testMap() {
        throw new MyException(500, "手动抛出");
    }

效果


本文为作者原创,手码不易,允许转载,转载后请以链接形式说明文章出处。

目录
相关文章
|
1月前
|
JavaScript 前端开发 Java
springboot从控制器请求至页面时js失效的解决方法
springboot从控制器请求至页面时js失效的解决方法
15 0
springboot从控制器请求至页面时js失效的解决方法
|
1月前
|
Java 数据库连接 mybatis
springboot访问jsp页面变成直接下载?
springboot访问jsp页面变成直接下载?
42 0
|
2月前
|
安全 Java Spring
SpringBoot2 | SpringBoot监听器源码分析 | 自定义ApplicationListener(六)
SpringBoot2 | SpringBoot监听器源码分析 | 自定义ApplicationListener(六)
47 0
|
1月前
|
JavaScript 前端开发
springboot+layui从控制器请求至页面时js失效的解决方法
springboot+layui从控制器请求至页面时js失效的解决方法
16 0
|
3月前
|
设计模式 Java 机器人
SpringBoot3自动配置流程 SPI机制 核心注解 自定义starter
SpringBoot3自动配置流程 SPI机制 核心注解 自定义starter
|
1月前
|
Java 数据库 数据安全/隐私保护
【SpringBoot】Validator组件+自定义约束注解实现手机号码校验和密码格式限制
【SpringBoot】Validator组件+自定义约束注解实现手机号码校验和密码格式限制
112 1
|
2天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
1月前
|
XML Java 数据格式
springboot 微服务项目如何集成 html 页面
springboot 微服务项目如何集成 html 页面
29 0
|
1月前
|
安全 Java 数据安全/隐私保护
SpringBoot启动后出现Please sign in页面
项目启动后,出现莫名其妙的Please sign in页面
45 0
|
1月前
|
前端开发 NoSQL Java
【SpringBoot】秒杀业务:redis+拦截器+自定义注解+验证码简单实现限流
【SpringBoot】秒杀业务:redis+拦截器+自定义注解+验证码简单实现限流