Spring Boot and custom 404 error page
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot is a powerful framework for building web applications and microservices in Java. It is designed to simplify the development process by providing default configurations and "starter" projects that encapsulate complex setup details. One of the common tasks in web applications is handling error messages, particularly implementing a custom 404 error page when a user lands on a non-existent endpoint.
Understanding Spring Boot
Spring Boot builds on top of the Spring framework and encapsulates a more opinionated setup approach. It simplifies dependency management, configuration, and deployment, making it easier for developers to get started with building web applications or REST APIs.
Key Features of Spring Boot
- Auto-Configuration: Automatically configures your application based on the JAR dependencies you add.
- Standalone: No need for an external application server.
- Spring Starter Projects: Provides a set of pre-defined templates for different projects.
- Production-Ready: Offers built-in metrics, health checks, and other enterprise-grade features.
- Consistent Development Environment: Provides the same environment everywhere, reducing environmental bugs and inconsistencies.
Custom 404 Error Page in Spring Boot
In web applications, HTTP status 404 is returned when a resource is not found on the server. Customizing this error page enhances the user experience by providing more context or guidance.
Default Error Handling in Spring Boot
Spring Boot handles exceptions thrown during request handling by delegating to a global error handling implementation. By default, Spring Boot creates a `BasicErrorController` for handling errors and displaying a standard error message formatted in JSON.
Creating a Custom 404 Error Page
To customize the 404 error page, you can create a custom error page to replace the default one. There are several methods to achieve this, including using a `ControllerAdvice` or overriding the default error view configurations.
Method 1: ControllerAdvice
A `ControllerAdvice` is a very flexible method because it enables you to centralize the error handling logic. Here is how you can create a custom handler for 404 errors:
- JSON Response: If you're building a RESTful API, you may want to return a JSON object for 404 errors. The setup is similar, but you return a `ResponseEntity` with a JSON payload.
- Logging: It's a good practice to log error messages using a logger to keep track of missing resources.
Related reading
- Spring boot and Flyway Clear database data before integration tests
- Spring Boot and how to configure connection details to MongoDB?
- Spring Boot and multiple external configuration files
- Spring Boot and multiple external configuration files
- Spring Boot and SQLite
- Spring Boot Application as a Daemon Service?
- Spring Boot application as a Service
- Spring Boot application gives 404 when deployed to Tomcat but works with embedded server

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.