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

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

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 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

Spring Boot Tutorial For Beginner- Spring Boot Full Course

This "Spring Boot Tutorial For Beginner- Spring Boot Full Course" video will help you learn Spring Boot Framework from scratch with examples. This Spring Tutorial is ideal for both beginners as well as professionals who want to master the Spring Boot Framework