diff --git a/spring-boot-modules/spring-boot-basic-customization/README.md b/spring-boot-modules/spring-boot-basic-customization/README.md index 1a899fd13a..98d1402631 100644 --- a/spring-boot-modules/spring-boot-basic-customization/README.md +++ b/spring-boot-modules/spring-boot-basic-customization/README.md @@ -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) - [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) + diff --git a/spring-boot-modules/spring-boot-basic-customization/pom.xml b/spring-boot-modules/spring-boot-basic-customization/pom.xml index ef56623775..4a45c548dd 100644 --- a/spring-boot-modules/spring-boot-basic-customization/pom.xml +++ b/spring-boot-modules/spring-boot-basic-customization/pom.xml @@ -26,6 +26,11 @@ spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-thymeleaf + + org.springframework.boot spring-boot-starter-test diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/errorhandling/ErrorHandlingApplication.java b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/errorhandling/ErrorHandlingApplication.java new file mode 100644 index 0000000000..5dd55ef077 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/errorhandling/ErrorHandlingApplication.java @@ -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); + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/errorhandling/controllers/IndexController.java b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/errorhandling/controllers/IndexController.java new file mode 100644 index 0000000000..1a8761e96b --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/errorhandling/controllers/IndexController.java @@ -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"; + } + +} diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/errorhandling/controllers/MyErrorController.java b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/errorhandling/controllers/MyErrorController.java new file mode 100644 index 0000000000..e002ac045d --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/errorhandling/controllers/MyErrorController.java @@ -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"; + } + +} diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/application-errorhandling.properties b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/application-errorhandling.properties new file mode 100644 index 0000000000..fba5c21cd4 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/application-errorhandling.properties @@ -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 \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error-404.html b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error-404.html new file mode 100644 index 0000000000..555cf52de4 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error-404.html @@ -0,0 +1,7 @@ + + + +

Sorry, we couldn't find the page you were looking for.

+

Go Home

+ + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error-500.html b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error-500.html new file mode 100644 index 0000000000..fc64c7e180 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error-500.html @@ -0,0 +1,9 @@ + + + +

Sorry, something went wrong!

+ +

We're fixing it.

+

Go Home

+ + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error.html b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error.html new file mode 100644 index 0000000000..4e953059e2 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error.html @@ -0,0 +1,8 @@ + + + +

Something went wrong!

+

Our Engineers are on it.

+

Go Home

+ + diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error/404.html b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error/404.html new file mode 100644 index 0000000000..df83ce219b --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/error/404.html @@ -0,0 +1,8 @@ + + + RESOURCE NOT FOUND + + +

404 RESOURCE NOT FOUND

+ + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/index.html b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/index.html new file mode 100644 index 0000000000..95c68c8600 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/templates/index.html @@ -0,0 +1,6 @@ + + +

Welcome Home

+

Success! It is working as we expected.

+ +