JAVA-86: spring-boot-custom-error-page
This commit is contained in:
parent
fff4be33f0
commit
9a4f4e47dc
@ -6,3 +6,6 @@ This module contains articles about Spring Boot customization
|
|||||||
|
|
||||||
- [How to Change the Default Port in Spring Boot](https://www.baeldung.com/spring-boot-change-port)
|
- [How to Change the Default Port in Spring Boot](https://www.baeldung.com/spring-boot-change-port)
|
||||||
- [Using Custom Banners in Spring Boot](https://www.baeldung.com/spring-boot-custom-banners)
|
- [Using Custom Banners in Spring Boot](https://www.baeldung.com/spring-boot-custom-banners)
|
||||||
|
- [Create a Custom FailureAnalyzer with Spring Boot](https://www.baeldung.com/spring-boot-failure-analyzer)
|
||||||
|
- [Spring Boot: Customize Whitelabel Error Page](https://www.baeldung.com/spring-boot-custom-error-page)
|
||||||
|
|
||||||
|
@ -26,6 +26,11 @@
|
|||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.errorhandling;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@ComponentScan(basePackages = "com.baeldung.errorhandling")
|
||||||
|
public class ErrorHandlingApplication {
|
||||||
|
|
||||||
|
public static void main(String [] args) {
|
||||||
|
System.setProperty("spring.profiles.active", "errorhandling");
|
||||||
|
SpringApplication.run(ErrorHandlingApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.errorhandling.controllers;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class IndexController {
|
||||||
|
|
||||||
|
@GetMapping(value = {"/", ""})
|
||||||
|
public String index() {
|
||||||
|
return "index";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(value = {"/server_error"})
|
||||||
|
public String triggerServerError() {
|
||||||
|
"ser".charAt(30);
|
||||||
|
return "index";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = {"/general_error"})
|
||||||
|
public String triggerGeneralError() {
|
||||||
|
return "index";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.errorhandling.controllers;
|
||||||
|
|
||||||
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
|
import javax.servlet.RequestDispatcher;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class MyErrorController implements ErrorController {
|
||||||
|
|
||||||
|
public MyErrorController() {}
|
||||||
|
|
||||||
|
@GetMapping(value = "/error")
|
||||||
|
public String handleError(HttpServletRequest request) {
|
||||||
|
|
||||||
|
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
|
||||||
|
|
||||||
|
if (status != null) {
|
||||||
|
|
||||||
|
Integer statusCode = Integer.valueOf(status.toString());
|
||||||
|
|
||||||
|
if(statusCode == HttpStatus.NOT_FOUND.value()) {
|
||||||
|
return "error-404";
|
||||||
|
}
|
||||||
|
else if(statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
|
||||||
|
return "error-500";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getErrorPath() {
|
||||||
|
return "/error";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
#server
|
||||||
|
server.port=9000
|
||||||
|
server.servlet-path=/
|
||||||
|
server.context-path=/
|
||||||
|
server.error.whitelabel.enabled=false
|
||||||
|
|
||||||
|
#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
|
||||||
|
|
||||||
|
#for Spring Boot 2.0+
|
||||||
|
#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
|
@ -0,0 +1,7 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1> Sorry, we couldn't find the page you were looking for. </h1>
|
||||||
|
<p><a href="/">Go Home</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,9 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1> Sorry, something went wrong! </h1>
|
||||||
|
|
||||||
|
<h2>We're fixing it.</h2>
|
||||||
|
<p><a href="/">Go Home</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,8 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1> Something went wrong! </h1>
|
||||||
|
<h2>Our Engineers are on it.</h2>
|
||||||
|
<p><a href="/">Go Home</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,8 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>RESOURCE NOT FOUND</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>404 RESOURCE NOT FOUND</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,6 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>Welcome Home</h1>
|
||||||
|
<p><strong>Success!</strong> It is working as we expected.</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user