diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 84dc43a605..f10fe5a909 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -28,7 +28,6 @@ org.springframework.boot spring-boot-starter-data-jpa - org.springframework.boot spring-boot-starter-actuator diff --git a/spring-boot/src/main/java/com/baeldung/errorhandling/ErrorHandlingApplication.java b/spring-boot/src/main/java/com/baeldung/errorhandling/ErrorHandlingApplication.java new file mode 100644 index 0000000000..45f9de78e4 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/errorhandling/ErrorHandlingApplication.java @@ -0,0 +1,26 @@ +package com.baeldung.errorhandling; + +import com.baeldung.autoconfiguration.MySQLAutoconfiguration; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.servlet.handler.HandlerMappingIntrospector; + +@SpringBootApplication(exclude = {MySQLAutoconfiguration.class}) +@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); + } + + @Bean(name = "mvcHandlerMappingIntrospector") + public HandlerMappingIntrospector mvcHandlerMappingIntrospector(ApplicationContext context) { + return new HandlerMappingIntrospector(context); + } + + +} diff --git a/spring-boot/src/main/java/com/baeldung/errorhandling/controllers/IndexController.java b/spring-boot/src/main/java/com/baeldung/errorhandling/controllers/IndexController.java new file mode 100644 index 0000000000..1a8761e96b --- /dev/null +++ b/spring-boot/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/src/main/java/com/baeldung/errorhandling/controllers/MyErrorController.java b/spring-boot/src/main/java/com/baeldung/errorhandling/controllers/MyErrorController.java new file mode 100644 index 0000000000..d0c548e7bb --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/errorhandling/controllers/MyErrorController.java @@ -0,0 +1,38 @@ +package com.baeldung.errorhandling.controllers; + +import org.springframework.boot.autoconfigure.web.ErrorController; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.servlet.RequestDispatcher; +import javax.servlet.http.HttpServletRequest; + +@Controller +public class MyErrorController implements ErrorController { + + public MyErrorController() {} + + @RequestMapping(value = "/error") + public String handleError(HttpServletRequest request) { + + Integer statusCode = + Integer.valueOf(request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE).toString()); + + if(statusCode == HttpStatus.NOT_FOUND.value()) { + return "error-404"; + } + else if(statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) { + return "error-500"; + } + else { + return "error"; + } + } + + @Override + public String getErrorPath() { + return "/error"; + } + +} diff --git a/spring-boot/src/main/resources/application-errorhandling.properties b/spring-boot/src/main/resources/application-errorhandling.properties new file mode 100644 index 0000000000..6ffcfbaf52 --- /dev/null +++ b/spring-boot/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/src/main/resources/templates/error-404.html b/spring-boot/src/main/resources/templates/error-404.html new file mode 100644 index 0000000000..3d3adaefa6 --- /dev/null +++ b/spring-boot/src/main/resources/templates/error-404.html @@ -0,0 +1,7 @@ + + + +

Sorry we couldn't find the resource you are looking for

+Go Home + + diff --git a/spring-boot/src/main/resources/templates/error-500.html b/spring-boot/src/main/resources/templates/error-500.html new file mode 100644 index 0000000000..979dd76764 --- /dev/null +++ b/spring-boot/src/main/resources/templates/error-500.html @@ -0,0 +1,7 @@ + + + +

Oops! We spilled something but we're fixing it

+Go Home + + diff --git a/spring-boot/src/main/resources/templates/error.html b/spring-boot/src/main/resources/templates/error.html new file mode 100644 index 0000000000..26d1a22b6c --- /dev/null +++ b/spring-boot/src/main/resources/templates/error.html @@ -0,0 +1,7 @@ + + + +

Something went wrong! Our Engineers are on it temp

+Go Home + + diff --git a/spring-boot/src/main/resources/templates/error/404.html b/spring-boot/src/main/resources/templates/error/404.html new file mode 100644 index 0000000000..df83ce219b --- /dev/null +++ b/spring-boot/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/src/main/resources/templates/index.ftl b/spring-boot/src/main/resources/templates/index.ftl new file mode 100644 index 0000000000..ce0505006e --- /dev/null +++ b/spring-boot/src/main/resources/templates/index.ftl @@ -0,0 +1,6 @@ + + + +

Welcome Home

+ + diff --git a/spring-boot/src/main/resources/templates/index.html b/spring-boot/src/main/resources/templates/index.html index 2c4387ed10..c1314558f6 100644 --- a/spring-boot/src/main/resources/templates/index.html +++ b/spring-boot/src/main/resources/templates/index.html @@ -4,7 +4,7 @@ - +

Welcome Home


×