Skip to content

Commit

Permalink
complete fix exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaxinyuuu committed Dec 12, 2021
1 parent 35d3249 commit e2be5db
Show file tree
Hide file tree
Showing 11 changed files with 768 additions and 578 deletions.
Binary file modified .DS_Store
Binary file not shown.
604 changes: 27 additions & 577 deletions .idea/workspace.xml

Large diffs are not rendered by default.

642 changes: 642 additions & 0 deletions log/MyBlog-dev.log

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/main/java/com/xiaxinyu/myblog/NotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.xiaxinyu.myblog;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)//将该异常作为资源找不到的状态供Springboot识别后判断是否返回404页面
public class NotFoundException extends RuntimeException{
public NotFoundException() {
}

public NotFoundException(String message) {
super(message);
}

public NotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.xiaxinyu.myblog.hander;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;


@ControllerAdvice
public class ControllerExceptionHandler {

private final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());

@ExceptionHandler(Exception.class)
//ModelAndView返回值为视图
public ModelAndView exceptionHandler(HttpServletRequest request, Exception e) throws Exception{

//判断是否存在状态标识,如果不存在则抛出异常
if(AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null){
throw e;
}

logger.error("Request URL : {}, Exception : {}", request.getRequestURI(),e);
//在控制台打印错误堆栈信息
ModelAndView mv = new ModelAndView();
mv.addObject("url",request.getRequestURI());
mv.addObject("exception",e);
//将异常url和异常类型传递给视图,可在error.html的源代码中以注释的形式显示异常信息
mv.setViewName("error/error");
return mv;
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/xiaxinyu/myblog/indexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
public class indexController {
@GetMapping("/")
public String index(){
int a = 9 / 0;
String blog = null;
if(blog == null)
{
throw new NotFoundException("博客不存在");
}
return "index";
}
}
19 changes: 19 additions & 0 deletions src/main/resources/templates/error/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>错误</title>
</head>
<body>
<h1>错误</h1>
<div>
<div th:utext="'&lt;!--' "th:remove="tag"></div>
<div th:utext="'Failed Request URL : ' + ${url}" th:remove="tag"></div>
<div th:utext="'Exception message : ' + ${exception.message}" th:remove="tag"></div>
<ul th:remove="tag">
<li th:each="st : ${exception.stackTrace}" th:remove="tag"><span th:utext="${st}" th:remove="tag"></span></li>
</ul>
<div th:utext="'--&gt;'" th:remove="tag"></div>
</div>
</body>
</html>
Binary file not shown.
Binary file not shown.
Binary file modified target/classes/com/xiaxinyu/myblog/indexController.class
Binary file not shown.
19 changes: 19 additions & 0 deletions target/classes/templates/error/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>错误</title>
</head>
<body>
<h1>错误</h1>
<div>
<div th:utext="'&lt;!--' "th:remove="tag"></div>
<div th:utext="'Failed Request URL : ' + ${url}" th:remove="tag"></div>
<div th:utext="'Exception message : ' + ${exception.message}" th:remove="tag"></div>
<ul th:remove="tag">
<li th:each="st : ${exception.stackTrace}" th:remove="tag"><span th:utext="${st}" th:remove="tag"></span></li>
</ul>
<div th:utext="'--&gt;'" th:remove="tag"></div>
</div>
</body>
</html>

0 comments on commit e2be5db

Please sign in to comment.