Trigger 404 in Spring-MVC controller?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In the realm of web development, Spring MVC (Model-View-Controller) is a robust framework that provides a declarative way to handle web requests by defining endpoints in @Controller classes. However, handling HTTP responses effectively, particularly triggering a 404 error when a resource cannot be found, is crucial. This article will delve into how to trigger a 404 error in a Spring-MVC controller, discussing the technical aspects and providing practical examples.
Spring MVC Overview
Spring MVC is a part of the Spring Framework that adheres to the Model-View-Controller design pattern. It decouples the different aspects of web application development such as handling requests, managing models and providing views to clients. Controllers act as the central part of this framework, linking user requests with back-end services.
Handling 404 Errors
A 404 error indicates that the server could not find the requested resource. There are scenarios in application development where a controller may need to explicitly return this status, such as when querying a database for non-existent records.
Techniques to Trigger a 404 Error
1. Using @ResponseStatus Annotation
Spring MVC provides the @ResponseStatus annotation to set the response status code for a controller method. For triggering a 404 error, you can annotate a method or exception class with @ResponseStatus(HttpStatus.NOT_FOUND).
2. Throwing ResourceNotFoundException in Controller
When a resource is not found, you can throw the custom exception within your controller method:
3. Using ResponseEntity
The ResponseEntity class provides an alternative way to manage HTTP responses and status codes. You can return a 404 status with an empty body if a resource is not found:
4. Mapping Exceptions Globally with @ControllerAdvice
@ControllerAdvice is a specialization of the @Component annotation that helps in handling exceptions globally. You can define exception handling logic across all controllers:
Summary Table
| Technique | Description | Code Example |
@ResponseStatus Annotation | Define response status directly on an exception class. | Custom exception class with @ResponseStatus. |
Throw ResourceNotFoundException | Trigger a 404 by throwing an exception in your controller. | Throw exception in controller method when resource is absent. |
ResponseEntity | Use ResponseEntity to return status codes and responses. | Return ResponseEntity with HttpStatus.NOT_FOUND. |
@ControllerAdvice | Handle exceptions globally across all controllers. | Define a class with @ControllerAdvice and handle exceptions. |
Conclusion
Managing 404 errors in Spring-MVC is an integral part of creating a user-friendly and robust web application. Using techniques like setting response statuses, throwing exceptions, and global exception handlers, you can ensure that users encounter informative responses when resources cannot be found. The choice of technique depends on the specific requirements of your application and the level of granularity you need in error handling.
The effective handling of 404 errors improves application reliability and provides clear feedback to users, enhancing the overall experience. Therefore, understanding these mechanisms is crucial for any developer working within the Spring MVC framework.
Related reading
- Trouble when changing Spring Boot version from 2.0.3.RELEASE to 2.1.0.BUILD-SNAPSHOT
- Trusting all certificates with okHttp
- Try-finally block prevents StackOverflowError
- Trying to use Spring Boot REST to Read JSON String from POST
- Turning a string into a Uri in Android
- Turning an ExecutorService to daemon in Java
- Tutorials on Core netty and Protobuf
- Type List vs type ArrayList in Java

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.