-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35d3249
commit e2be5db
Showing
11 changed files
with
768 additions
and
578 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/xiaxinyu/myblog/hander/ControllerExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="'<!--' "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="'-->'" th:remove="tag"></div> | ||
</div> | ||
</body> | ||
</html> |
Binary file not shown.
Binary file added
BIN
+1.95 KB
target/classes/com/xiaxinyu/myblog/hander/ControllerExceptionHandler.class
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="'<!--' "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="'-->'" th:remove="tag"></div> | ||
</div> | ||
</body> | ||
</html> |