Wednesday 4 September 2019

Spring boot: Customize Whitelabel Error Page

Spring boot provides a Whitelabel Error Page, if it is unable to identify proper handler for a request.


We can customize Whitelable error page by implementing ErrorController interface.
package com.sample.app.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WhiteLabelController implements ErrorController {

    private static final String PATH = "/error";

    @RequestMapping(value = PATH)
    public String handleError() {
        return "Error handling";
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }
}

After adding above snippet to your application, you can see below kind of screen on error scenarios.

Let’s further enhance this by redirecting to specific error pages depends on their error type.
@RequestMapping(PATH)
public ModelAndView handleError(HttpServletRequest request, Model model) {
    Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);

    if (status == null) {
        ModelAndView modelAndView = new ModelAndView("error");
        return modelAndView;
    }

    Integer statusCode = Integer.valueOf(status.toString());

    if (statusCode == HttpStatus.NOT_FOUND.value()) {
        ModelAndView modelAndView = new ModelAndView("404");
        return modelAndView;

    } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
        ModelAndView modelAndView = new ModelAndView("500");
        return modelAndView;
    }

    ModelAndView modelAndView = new ModelAndView("error");
    return modelAndView;
}

In case of 404 error code, 404.jsp file will be rendered, 500, 500.jsp file will be rendered and other errors ‘error.jsp’ will be rendered.

Find the below working application.

App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

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

}

HomeController.java
package com.sample.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @RequestMapping("/")
    public String homePage() {
        return "Welcome to Spring boot Application Development";
    }

}

WhiteLabelController.java
package com.sample.app.controller;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class WhiteLabelController implements ErrorController {

    private static final String PATH = "/error";

    @RequestMapping(PATH)
    public ModelAndView handleError(HttpServletRequest request, Model model) {
        Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);

        if (status == null) {
            ModelAndView modelAndView = new ModelAndView("error");
            return modelAndView;
        }

        Integer statusCode = Integer.valueOf(status.toString());

        if (statusCode == HttpStatus.NOT_FOUND.value()) {
            ModelAndView modelAndView = new ModelAndView("404");
            return modelAndView;

        } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
            ModelAndView modelAndView = new ModelAndView("500");
            return modelAndView;
        }

        ModelAndView modelAndView = new ModelAndView("error");
        return modelAndView;
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }

}

application.properties
# Properties to redirect to view form controller
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp

404.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>404</title>
</head>
<body>
    <h1>
        Page not found
    </h1>
</body>
</html>

500.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>500</title>
</head>
<body>
    <h1>Internal Server Error Occurred</h1>
</body>
</html>

error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>Unable to process your request</h1>
</body>
</html>

pom.xml
<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>springbootMisc</groupId>
    <artifactId>springbootMisc</artifactId>
    <version>1</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <packaging>jar</packaging>

    <dependencies>

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

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

    </dependencies>

</project>

Total project looks like below.
Open the url ‘http://localhost:8080/greetMe’ in browser, you can see 404.jsp file.


You can download the complete working application from this link.
    

Previous                                                    Next                                                    Home

No comments:

Post a Comment