Merge pull request #3932 from SeunMatt/master
Example Code for BAEL-1682
This commit is contained in:
commit
0c6c5b9286
|
@ -28,7 +28,6 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</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,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
|
|
@ -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>
|
|
@ -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>
|
|
@ -0,0 +1,7 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<h1>Something went wrong! Our Engineers are on it temp</h1>
|
||||
<a href="/">Go Home</a>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>RESOURCE NOT FOUND</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>404 RESOURCE NOT FOUND</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,6 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<h1>Welcome Home</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -4,7 +4,7 @@
|
|||
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>Welcome Home</h1>
|
||||
<div class="container"><br/>
|
||||
<div class="alert alert-success">
|
||||
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
|
||||
|
|
Loading…
Reference in New Issue