Error Handling


1. Screenshot

2.  POM File
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.errorhandling</groupId>
<artifactId>internationalization</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>errorhandling</name>
<description>errorhandling project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>


3.ErrorhandlingApplication Class
package com.errorhandling;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;


@SpringBootApplication
public class ErrorhandlingApplication {


public static void main(String[] args) {
SpringApplication.run(ErrorhandlingApplication.class, args);
}
}

4. Error.html file
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Error Handling Demo</title>
<link rel="stylesheet" href="../static/css/blog.css" th:href="@{/css/blog.css}"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />

<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>

<div class="container">
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-5" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">therealdanvega.com</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-5">
            <p class="navbar-text navbar-right">Signed in as <a href="#" class="navbar-link">Guest User</a></p>
        </div>
    </div>
</nav>
<h2>Standard Error Handler</h2>
<p th:text="${error}">error</p>
<p th:text="${message}">message</p>
</div>

</body>
</html>

5. blog.css CSS file

 .navbar {
margin-top:20px;
}

header h1 {
font-family: 'Exo', sans-serif;
font-size: 38px;
}

p, address {
font-family: 'Roboto', sans-serif;
}

6.  application.properties file 
   server.port=9009
   spring.thymeleaf.cache=false

7. Run File  Following output result will be displayed.


-----------------------------------------------------------------
8.  Create CustomErrorController class
package com.errorhandling.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CustomErrorController implements ErrorController {
private static final String ERROR_PATH = "/error";
private static final String ERROR_TEMPLATE = "customError";
private final ErrorAttributes errorAttributes;

@Autowired
public CustomErrorController(ErrorAttributes errorAttributes) {
this.errorAttributes = errorAttributes;
}

@RequestMapping(ERROR_PATH)
//@ResponseBody
public String handleError(Model model,HttpServletRequest request) {
   Map<String,Object> error = getErrorAttributes(request, true);
model.addAttribute("timestamp", error.get("timestamp"));
model.addAttribute("status", error.get("status"));
model.addAttribute("error", error.get("error"));
model.addAttribute("message", error.get("message"));
model.addAttribute("path", error.get("path"));
return ERROR_TEMPLATE;
}

@Override
public String getErrorPath() {
return "/error";
}
private Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
// Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
// Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
WebRequest requestAttributes = new ServletWebRequest(request);
return this.errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);

}

}

9. Create customError.html file inside resources/template s

<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymleaf.org">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Error Handling Demo</title>
<link rel="stylesheet" href="../static/css/blog.css" th:href="@{/css/blog.css}"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />

<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>

<div class="container">
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-5" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">therealdanvega.com</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-5">
            <p class="navbar-text navbar-right">Signed in as <a href="#" class="navbar-link">Guest User</a></p>
        </div>
    </div>
</nav>
<h2>Error Handler</h2>
<table class="table table-striped">
<thead>
<tr>
<td>Key</td>
<td>Value</td> 
</tr>
</thead>
<tbody>
<tr>
<td>TimeStamp</td>
<td th:text="${timestamp}"></td>
</tr>
<tr>
<td>Status</td>
<td th:text="${status}"></td>
</tr>
<tr>
<td>Message</td>
<td th:text="${message}"></td>
</tr>
<tr>
<td>Error</td>
<td th:text="${error}"></td>
</tr>
<tr>
<td>Path</td>
<td th:text="${path}"></td>
</tr>
</tbody>
</table>
</div>

</body>
</html>

10. Now the Program.

11. 



Comments

Popular posts from this blog