added example code for BAEL-1682
This commit is contained in:
parent
e8c4bb6308
commit
099278bfb8
@ -20,6 +20,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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,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";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
#server
|
||||||
|
server.port=9000
|
||||||
|
server.servlet-path=/
|
||||||
|
server.context-path=/
|
||||||
|
#server.error.whitelabel.enabled=false
|
||||||
|
#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
|
||||||
|
|
||||||
|
#freemarker
|
||||||
|
spring.freemarker.suffix=.ftl
|
7
spring-boot/src/main/resources/templates/error-404.ftl
Normal file
7
spring-boot/src/main/resources/templates/error-404.ftl
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>Sorry we couldn't find the resource you are looking for</h1>
|
||||||
|
<a href="/">Go Home</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
7
spring-boot/src/main/resources/templates/error-500.ftl
Normal file
7
spring-boot/src/main/resources/templates/error-500.ftl
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>Oops! We spilled something but we're fixing it</h1>
|
||||||
|
<a href="/">Go Home</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
7
spring-boot/src/main/resources/templates/error.ftl
Normal file
7
spring-boot/src/main/resources/templates/error.ftl
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>Something went wrong! Our Engineers are on it</h1>
|
||||||
|
<a href="/">Go Home</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
6
spring-boot/src/main/resources/templates/index.ftl
Normal file
6
spring-boot/src/main/resources/templates/index.ftl
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>Welcome Home</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user