前言
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
一、全局异常处理
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这里内嵌了腾讯公益活动,链接附在下方了。