Skip to content

Commit 099278b

Browse files
committed
added example code for BAEL-1682
1 parent e8c4bb6 commit 099278b

9 files changed

Lines changed: 130 additions & 0 deletions

File tree

‎spring-boot/pom.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
<groupId>org.springframework.boot</groupId>
2121
<artifactId>spring-boot-starter-thymeleaf</artifactId>
2222
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-freemarker</artifactId>
26+
</dependency>
2327
<dependency>
2428
<groupId>org.springframework.boot</groupId>
2529
<artifactId>spring-boot-starter-web</artifactId>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#server
2+
server.port=9000
3+
server.servlet-path=/
4+
server.context-path=/
5+
#server.error.whitelabel.enabled=false
6+
#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
7+
8+
#freemarker
9+
spring.freemarker.suffix=.ftl
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<h1>Something went wrong! Our Engineers are on it</h1>
5+
<a href="/">Go Home</a>
6+
</body>
7+
</html>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<h1>Welcome Home</h1>
5+
</body>
6+
</html>

0 commit comments

Comments
 (0)