1、总览
2、代码
1)、pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies>
2)、application.properties
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
server.error.whitelabel.enabled=false
server.error.include-stacktrace=always
3)、java代码
package com.ebc.controller; import com.ebc.error.ForbiddenException; import com.ebc.error.NotYetImplemented; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; @Controller public class MyController { /** * response status code=500,导航到5xx.jsp */ @RequestMapping("/") public void handleRequest() { throw new RuntimeException("test exception"); } /** * response status code=501,导航到5xx.jsp */ @RequestMapping("/app") public void handleAppInfoRequest() throws NotYetImplemented { throw new NotYetImplemented("The request page is not yet implemented"); } /** * response status code=403,因为没有403.jsp,因此会导航到error.jsp */ @RequestMapping("/admin") public void handleAdminRequest() throws ForbiddenException { throw new ForbiddenException("The requested page is forbidden"); } /** * 返回501,但是无法导航到5xx.jsp页面 */ @RequestMapping("/some") @ResponseStatus(HttpStatus.NOT_IMPLEMENTED) public void handleSomeRequest() { } /** * 返回501,但是无法导航到5xx.jsp页面 */ @RequestMapping("/some2") public ResponseEntity<?> handleSomeInfoRequest() { ResponseEntity<?> re = new ResponseEntity<>((Object)null, HttpStatus.NOT_IMPLEMENTED); return re; } } package com.ebc.error; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.FORBIDDEN) public class ForbiddenException extends Exception { public ForbiddenException(String message) { super(message); } } package com.ebc.error; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.NOT_IMPLEMENTED) public class NotYetImplemented extends Exception { public NotYetImplemented(String message) { super(message); } }
4)、5xx.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <html> <head> <style> table td{ vertical-align:top; border:solid 1px #888; padding:10px; } </style> </head> <body> <h1>My 5xx Error Page</h1> <table> <tr> <td>Date</td> <td>${timestamp}</td> </tr> <tr> <td>Error</td> <td>${error}</td> </tr> <tr> <td>Status</td> <td>${status}</td> </tr> <tr> <td>Message</td> <td>${message}</td> </tr> <tr> <td>Exception</td> <td>${exception}</td> </tr> <tr> <td>Trace</td> <td> <pre>${trace}</pre> </td> </tr> </table> </body> </html>
5)、404.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <html> <head> <style> table td{ vertical-align:top; border:solid 1px #888; padding:10px; } </style> </head> <body> <h1>My 404 Error Page</h1> <table> <tr> <td>Date</td> <td>${timestamp}</td> </tr> <tr> <td>Error</td> <td>${error}</td> </tr> <tr> <td>Status</td> <td>${status}</td> </tr> <tr> <td>Message</td> <td>${message}</td> </tr> <tr> <td>Exception</td> <td>${exception}</td> </tr> <tr> <td>Trace</td> <td> <pre>${trace}</pre> </td> </tr> </table> </body> </html>
6)、error.jsp(在views文件夹下,即application.properties 中的spring.mvc.view.prefix=/WEB-INF/views/)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <html> <head> <style> table td{ vertical-align:top; border:solid 1px #888; padding:10px; } </style> </head> <body> <h1>My Custom Global Error Page</h1> <table> <tr> <td>Date</td> <td>${timestamp}</td> </tr> <tr> <td>Error</td> <td>${error}</td> </tr> <tr> <td>Status</td> <td>${status}</td> </tr> <tr> <td>Message</td> <td>${message}</td> </tr> <tr> <td>Exception</td> <td>${exception}</td> </tr> <tr> <td>Trace</td> <td> <pre>${trace}</pre> </td> </tr> </table> </body> </html>
3、执行
发现,没有导航到5xx.jsp,只是返回了501
输入一个不存在的资源,导航到了404.jsp了。