File tree Expand file tree Collapse file tree
java/com/baeldung/errorhandling Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2828 <groupId >org.springframework.boot</groupId >
2929 <artifactId >spring-boot-starter-data-jpa</artifactId >
3030 </dependency >
31-
3231 <dependency >
3332 <groupId >org.springframework.boot</groupId >
3433 <artifactId >spring-boot-starter-actuator</artifactId >
Original file line number Diff line number Diff line change 1+ package com .baeldung .errorhandling ;
2+
3+ import com .baeldung .autoconfiguration .MySQLAutoconfiguration ;
4+ import org .springframework .boot .SpringApplication ;
5+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
6+ import org .springframework .context .ApplicationContext ;
7+ import org .springframework .context .annotation .Bean ;
8+ import org .springframework .context .annotation .ComponentScan ;
9+ import org .springframework .web .servlet .handler .HandlerMappingIntrospector ;
10+
11+ @ SpringBootApplication (exclude = {MySQLAutoconfiguration .class })
12+ @ ComponentScan (basePackages = "com.baeldung.errorhandling" )
13+ public class ErrorHandlingApplication {
14+
15+ public static void main (String [] args ) {
16+ System .setProperty ("spring.profiles.active" , "errorhandling" );
17+ SpringApplication .run (ErrorHandlingApplication .class , args );
18+ }
19+
20+ @ Bean (name = "mvcHandlerMappingIntrospector" )
21+ public HandlerMappingIntrospector mvcHandlerMappingIntrospector (ApplicationContext context ) {
22+ return new HandlerMappingIntrospector (context );
23+ }
24+
25+
26+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .errorhandling .controllers ;
2+
3+ import org .springframework .stereotype .Controller ;
4+ import org .springframework .web .bind .annotation .GetMapping ;
5+ import org .springframework .web .bind .annotation .PostMapping ;
6+
7+ @ Controller
8+ public class IndexController {
9+
10+ @ GetMapping (value = {"/" , "" })
11+ public String index () {
12+ return "index" ;
13+ }
14+
15+ @ GetMapping (value = {"/server_error" })
16+ public String triggerServerError () {
17+ "ser" .charAt (30 );
18+ return "index" ;
19+ }
20+
21+ @ PostMapping (value = {"/general_error" })
22+ public String triggerGeneralError () {
23+ return "index" ;
24+ }
25+
26+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .errorhandling .controllers ;
2+
3+ import org .springframework .boot .autoconfigure .web .ErrorController ;
4+ import org .springframework .http .HttpStatus ;
5+ import org .springframework .stereotype .Controller ;
6+ import org .springframework .web .bind .annotation .RequestMapping ;
7+
8+ import javax .servlet .RequestDispatcher ;
9+ import javax .servlet .http .HttpServletRequest ;
10+
11+ @ Controller
12+ public class MyErrorController implements ErrorController {
13+
14+ public MyErrorController () {}
15+
16+ @ RequestMapping (value = "/error" )
17+ public String handleError (HttpServletRequest request ) {
18+
19+ Integer statusCode =
20+ Integer .valueOf (request .getAttribute (RequestDispatcher .ERROR_STATUS_CODE ).toString ());
21+
22+ if (statusCode == HttpStatus .NOT_FOUND .value ()) {
23+ return "error-404" ;
24+ }
25+ else if (statusCode == HttpStatus .INTERNAL_SERVER_ERROR .value ()) {
26+ return "error-500" ;
27+ }
28+ else {
29+ return "error" ;
30+ }
31+ }
32+
33+ @ Override
34+ public String getErrorPath () {
35+ return "/error" ;
36+ }
37+
38+ }
Original file line number Diff line number Diff line change 1+ # server
2+ server.port =9000
3+ server.servlet-path =/
4+ server.context-path =/
5+ # server.error.whitelabel.enabled=false
6+
7+ # spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
8+
9+ # for Spring Boot 2.0+
10+ # spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+ < body >
4+ < h1 > Sorry we couldn't find the resource you are looking for</ h1 >
5+ < a href ="/ "> Go Home</ a >
6+ </ body >
7+ </ html >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+ < body >
4+ < h1 > Oops! We spilled something but we're fixing it</ h1 >
5+ < a href ="/ "> Go Home</ a >
6+ </ body >
7+ </ html >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+ < body >
4+ < h1 > Something went wrong! Our Engineers are on it temp</ h1 >
5+ < a href ="/ "> Go Home</ a >
6+ </ body >
7+ </ html >
Original file line number Diff line number Diff line change 1+ < html >
2+ < head >
3+ < title > RESOURCE NOT FOUND</ title >
4+ </ head >
5+ < body >
6+ < h1 > 404 RESOURCE NOT FOUND</ h1 >
7+ </ body >
8+ </ html >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ <html >
3+ <body >
4+ <h1 >Welcome Home</h1 >
5+ </body >
6+ </html >
You can’t perform that action at this time.
0 commit comments