Error page in Spring Boot application with CookieLocaleResolver

Error page in Spring Boot application with CookieLocaleResolver

I Have a Spring Boot application which has the&nbsp;<code>org.springframework.web.servlet.i18n.CookieLocaleResolver</code>&nbsp;for locale resolver. If there is a invalid language cookie like&nbsp;<code>!en</code>&nbsp;then there will be an exception&nbsp;<code>java.lang.IllegalArgumentException: Locale part &amp;quot;!en&amp;quot; contains invalid characters</code>.

I Have a Spring Boot application which has the org.springframework.web.servlet.i18n.CookieLocaleResolver for locale resolver. If there is a invalid language cookie like !en then there will be an exception java.lang.IllegalArgumentException: Locale part &quot;!en&quot; contains invalid characters.

The problem is this exception is not handled by Spring Boot instead it is forwarded to Servlet container. So the default error page of the the container is shown (In my case it is JBoss EAP 6) which will show the stacktrace.

Other exceptions from the controllers are handled properly. For example I have a controller mapping which will throw / by zero error which is handled properly.

I have tried error page configuration in web.xml as follows.

<error-page>
    <location>/500</location>
</error-page>

And mapped both /error and /500 to a MVC controller as follows.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller public class CustomErrorController extends AbstractErrorController { public static final String ERROR_500 = "/500"; private static final String ERROR_PATH= "/error";

@Autowired
public CustomErrorController(ErrorAttributes errorAttributes) {
    super(errorAttributes);
}


/**
 * Responsible for handling all errors and throw especial exceptions
 * for some HTTP status codes. Otherwise, it will return a map that
 * ultimately will be converted to a json error.
 */
@RequestMapping({ERROR_PATH,ERROR_500})
public ResponseEntity&lt;?&gt; handleErrors(HttpServletRequest request) {
    return ResponseEntity.status(getStatus(request)).body(getErrorAttributes(request, false));
}

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

}

But still I'm getting the container's default error page. How to resolve this.

Test application code is here

spring-boot

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Spring Boot Tutorials - Spring Boot Full Course

Spring Boot Tutorials | Full Course - What is Spring? Spring Boot is an open source Java-based framework used to create a Micro Service. Spring Boot contains a comprehensive infrastructure support for developing a micro service and enables you to develop enterprise-ready applications that you can “just run”.

Spring vs Spring Boot

We will see how Spring and Spring Boot can be compared. Programmers need not start from scratch when they want to work on some projects.

What Is Spring Boot ?

What Is Spring Boot ? I will explain you what is sprint boot. This is introduction to spring boot and why spring boot is used.

Learn Hibernate and Spring for Beginners (Includes Spring Boot)

Spring 5: Learn Spring 5 Core, AOP, Spring MVC, Spring Security, Spring REST, Spring Boot 2, Thymeleaf, JPA & Hibernate

Spring Boot - Spring Security + JWT Complete Tutorial With Example

Learn how to implement spring security using JWT (JSON Web Token). What is JWT? Why JWT? JSON Web Token (JWT) is a compact URL-safe means of representing claims to be transferred between two parties. Spring Boot - Spring Security + JWT Complete Tutorial With Example