Spring ResponseStatusException does not return reason
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Framework is widely used in Java applications to develop scalable and high-performing web applications. It provides a rich set of features including Spring MVC for building web applications. When handling exceptions in Spring MVC, developers might use ResponseStatusException to set the HTTP status and reason. However, some developers might encounter an unexpected issue where the reason specified in ResponseStatusException is not returned. This article delves into why this might occur and how to address it effectively.
Understanding ResponseStatusException
ResponseStatusException is a part of the Spring Web module that enables developers to set both an HTTP status code and a reason when an exception occurs. It is a convenient way to provide clients with detailed error information and HTTP response status. The basic usage involves creating an instance of ResponseStatusException and passing an HTTP status and an optional reason string.
Example Usage
In the above example, when this exception is thrown, it is expected that the client receives a 404 status code along with the reason "Resource not found".
Common Issue: Reason Not Returning
In practice, developers sometimes encounter an issue where the reason does not appear in the HTTP response. Only the status code is returned. This can lead to confusion for both developers and clients expecting detailed error information.
Technical Explanation
The root cause of the reason not being returned is often tied to the way Spring Boot handles error responses. By default, error details (such as reasons) are not included in responses due to security considerations. Revealing too much information in error responses can lead to security vulnerabilities by exposing sensitive details about the server or application.
Spring Boot error handling policy is defined in the ErrorAttributes interface, which typically suppresses detailed information to prevent security risks.
Spring Boot Configuration for Error Handling
In Spring Boot, the handling of error attributes is governed by the properties prefixed with server.error. To change whether or not the reason is part of the response, developers need to adjust the server.error.include-message property.
Configuration Example
Setting include-message to always would ensure that the reason message you provide in ResponseStatusException is included in the response.
A Custom Error Handling Example
Another approach is to use a custom error attribute configuration to include the reason in error responses:
This component extends the default error handling to specifically add the reason message to the error attributes that are returned in the response.
Best Practices and Considerations
- Security: Always consider the security implications of returning detailed error messages. Only include detailed messages in development environments or for internal APIs where security risks are mitigated.
- Consistency: Ensure consistency across your application in how errors and exceptions are handled. Use a centralized error handling mechanism to avoid discrepancies.
- Testing: Perform thorough testing of error scenarios to ensure users receive meaningful and accurate responses from your API.
Summary Table
Below is a summary table highlighting the key points discussed:
| Key Point | Explanation |
Purpose of ResponseStatusException | Sets HTTP status and reason for exceptions in Spring MVC |
| Common Issue | Response does not include reason due to security-related configuration |
| Default Spring Boot Behavior | Error attributes are suppressed to prevent revealing sensitive information |
| Configuration Change | Use server.error.include-message: always to include error reasons |
| Custom Handler Usage | Implement custom ErrorAttributes to capture and return specific error details |
| Best Practice | Be cautious with detailed error messages and ensure consistent error handling |
In conclusion, while ResponseStatusException offers a convenient way to handle exceptions with custom reasons, developers must be mindful of the default configurations in Spring Boot that prioritize security over detailed error messages. Adjustments to these settings should be made judiciously, keeping in mind the trade-offs between information detail and security.
Related reading
- Spring rest controller not returning html
- Spring RestTemplate - how to enable full debugging/logging of requests/responses?
- Spring RestTemplate Exponential Backoff retry policy
- Spring RestTemplate GET with parameters
- Spring RestTemplate throws exception Broken pipe, while calling different Rest API Synchronously
- Spring Scheduling - Cron expression for everyday at midnight not working?
- Spring Security 5 No Beans of type BCryptPasswordEncoder found
- Spring Security 5 Replacement for OAuth2RestTemplate

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.