【第19章】spring-mvc之全局异常处理


前言

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


一、全局异常处理

1. 前端

<%--
  Created by IntelliJ IDEA.
  User: 张军国001
  Date: 2024/5/5
  Time: 11:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>未知的错误</title>
</head>
<body>
<p>错误码:${code}</p>
<p>错误信息:${message}</p>
</body>
</html>

2. 后端

全局异常处理

package org.example.springmvc.exception;

import com.alibaba.fastjson2.support.spring6.webservlet.view.FastJsonJsonView;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Create by zjg on 2024/5/5
 */
@ControllerAdvice
@Log4j2
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ModelAndView exception(HttpServletRequest request,Exception exception){
        trace(exception);
        ModelAndView modelAndView ;
        String accept = request.getHeader("accept");
        if(accept.contains("application/json")){
            modelAndView=new ModelAndView(new FastJsonJsonView());
        }else{
            modelAndView = new ModelAndView("params/error");
        }
        modelAndView.addObject("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
        modelAndView.addObject("message",exception.getMessage());
        return modelAndView;
    }
    private void trace(Exception exception){
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer=new PrintWriter(stringWriter);
        exception.printStackTrace(writer);
        log.error(stringWriter);
    }
}

异常类

package org.example.springmvc.exception;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Create by zjg on 2024/5/5
 */
@Controller
public class ExceptionController {
    @RequestMapping("/error01")
    public JSONObject error01(){
        int i=1/0;
        return new JSONObject();
    }
    @RequestMapping("/error02")
    public String error02(){
        int i=1/0;
        return "params/error";
    }
}

二、常见错误页

1.增加界面

在这里插入图片描述

2.web.xml

<error-page>
    <error-code>400</error-code>
    <location>/resources/error/400.html</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/resources/error/404.html</location>
</error-page>

3.异常处理

@ExceptionHandler(HttpMessageNotReadableException.class)
public ModelAndView exception(NoResourceFoundException exception){
    trace(exception);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("code", HttpStatus.BAD_REQUEST.value());
    modelAndView.addObject("message",exception.getMessage());
    return modelAndView;
}
@ExceptionHandler(NoResourceFoundException.class)
public ModelAndView noResourceFoundException(NoResourceFoundException exception){
    trace(exception);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("code", HttpStatus.NOT_FOUND.value());
    modelAndView.addObject("message",exception.getMessage());
    return modelAndView;
}

4.效果

在这里插入图片描述
在这里插入图片描述

404这里内嵌了腾讯公益活动,链接附在下方了。


总结

回到顶部
https://github.com/songjinzhong/404html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值