How to handle Whitelabel 404 error in Spring Boot without using Thymeleaf or other template engines

Asked
Active3 hr before
Viewed126 times

10 Answers

errorhandle
90%

I have a Spring Boot + Spring Security application with React JS for client. I want to have a custom styled 404 error page in it without using Thymeleaf., What would land vehicles designed to operate in a perpetual hurricane look like? ,Baeldung.com indicates src/main/resources/public/errors, which does not work, at least with SpringBoot 2.1.x. Use the singular error and you will be good.

Pretag
 Pretag team - issue, fix, solve, resolve
88%

Please enable Cookies and reload the page.,Please stand by, while we are checking your browser...,Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.

Pretag
 Pretag team - issue, fix, solve, resolve
72%

Without using a Thymeleaf template engine, we can place a generic custom error page in a src/main/resources/public/errors directory. , A generic error page using a template can be placed in the src/main/resources/templates/ directory. , A specific error page using a template can be placed in the src/main/resources/templates/error/ directory.

WhiteLabel Error Page is a generic Spring Boot error page that is displayed when no custom error page is present.

server.error.whitelabel.enabled = false
load more v
65%

If not using Thymeleaf template engine then you can place a generic custom error page in  src/main/resources/public/errors directory. ,Generic error page using template can be placed in directory src/main/resources/templates/ ,Specific error page using a template is placed in directory.  src/main/resources/templates/error/ 

Generic error page for 404 error : resources/public/errors/404.html

<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="UTF-8">
   <title>404 - Resource not found</title>
</head>

<body>

   <h2>404 - Resource not found</h2>

   <p>
      The requested resource was not found
   </p>

</body>

</html>
load more v
75%

Let’s create a new error page which we will use in this new controller method.,When we run our application this time, we will have a different error page displayed to the customer.,Let’s create our custom error.html and place it under the <em>resources/templates</em> directory.

Spring Boot provides an application.properties (or YAML) file to easily configure/change your application. We can use the same property file to disable this error page globally. Set server.error.whitelabel.enabled to false to achieve this.

# Whether to enable the
default error page displayed in browsers in
   case ofa server error.
server.error.whitelabel.enabled = false
load more v
40%

We can resolve the Whitelabel Error Page error in 3 ways:,Spring Boot Version <= 1.2 then use error.whitelabel.enabled = false,Customize the ‘whitelabel’ Error Page

package org.websparrow.controller;

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

@RestController
public class ErrorrHandlerController implements ErrorController {

   @GetMapping("/error")
   public String customError() {
      return "The link you followed may be broken, or the page may have been removed.";
   }

   @Override
   public String getErrorPath() {
      return "/error";
   }
}
<!DOCTYPE html>
<html>
<title>Error</title>

<body>

   <h1>Something went wrong!</h1>
   <p>The link you followed may be broken, or the page may have been removed.</p>

</body>

</html>

Since we’re using Thymeleaf template engine to display the custom error page. Add the Thymeleaf dependency in the pom.xml:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
   <version>2.1.8.RELEASE</version>
</dependency>
#Disable Whitelabel Error Page
server.error.whitelabel.enabled = false
@SpringBootApplication(exclude = {
   ErrorMvcAutoConfiguration.class
})
public class Main {

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

}
load more v
22%

Create a web-based spring boot application in the spring tool suite. In the pom.xml file, add spring-boot-starter-web dependency. The maven dependence will be as shown in the code below.,The spring boot main class will be created in the “com.yawintutor.application” package. The main class will be shown in the code below. ,Add the following line to the application.properties, restart the spring boot application. The default Whitelable error page will disappear and the blank page will be displayed.

Whitelabel Error Page
This application has no explicit mapping
for / error, so you are seeing this as a fallback.

Fri Apr 10 22: 45: 19 IST 2020
There was an unexpected error(type = Not Found, status = 404).
No message available
load more v
60%

1. Let’s start by adding Thymeleaf template engine dependency to pom.xml.,That’s all about displaying custom error pages in Spring Boot.,By default, Spring Boot displays a Whitelabel error page if we encounter a server error. To specify the custom error logic, we need a @Controller that handles the /error path or a View that resolves with a name of error. Let’s discuss both approaches in detail:

Pretag
 Pretag team - issue, fix, solve, resolve
48%

If you haven’t yet developed a Spring Boot web application you can follow the "Hello World!" example in the Getting started section.,Spring Boot provides auto-configuration for Spring MVC that works well with most applications.,Spring Boot includes auto-configuration support for the following templating engines:

If you need to add or customize converters you can use Spring Boot’s HttpMessageConverters class:

import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.*;
import org.springframework.http.converter.*;

@Configuration
public class MyConfiguration {

   @Bean
   public HttpMessageConverters customConverters() {
      HttpMessageConverter < ? > additional = ...
         HttpMessageConverter < ? > another = ...
         return new HttpMessageConverters(additional, another);
   }

}
load more v
23%

Entering a URL which is not mapped by our controller, will return whitelabel page with 404,At URI /test, where the mapped handler method throws NullPointerException:,As seen above the whitelabel page is not used, also the error is not even handled by the tomcat container's default error handling mechanism.

@Controller
public class MyController {

   @RequestMapping("/")
   public String handler(Model model) {
      model.addAttribute("msg",
         "a spring-boot example");
      return "myPage";
   }

   @RequestMapping("/test")
   public void handler2() {
      throw new RuntimeException("exception from handler2");
   }
}
load more v

Other "error-handle" queries related to "How to handle Whitelabel 404 error in Spring Boot without using Thymeleaf or other template engines"