Using Spring @ResponseStatus to Set HTTP Status Code

1. Introduction

In Spring MVC, we have many ways to set the status code of an HTTP response.

In this short tutorial, we will see the most straightforward way: using the @ResponseStatus annotation.

2. On Controller Methods

When an endpoint returns successfully, Spring provides an HTTP 200 (OK) response.

If we want to specify the response status of a controller method, we can mark that method with @ResponseStatus. It has two interchangeable arguments for the desired response status: code, and value. For example, we can indicate that the server refuses to brew coffee because it is a teapot:

@ResponseStatus(HttpStatus.I_AM_A_TEAPOT)
void teaPot() {}

When we want to signal an error, we can provide an error message via the reason argument:

@ResponseStatus(HttpStatus.BAD_REQUEST, reason = "Some parameters are invalid")
void onIllegalArgumentException(IllegalArgumentException exception) {}

Note, that when we set reason, Spring calls HttpServletResponse.sendError(). Therefore, it will send an HTML error page to the client, which makes it a bad fit for REST endpoints.

Also note, that Spring only uses @ResponseStatus, when the marked method completes successfully (without throwing an Exception).

3. With Error Handlers

We have three ways to use @ResponseStatus to convert an Exception to an HTTP response status:

  • using @ExceptionHandler
  • using @ControllerAdvice
  • marking the Exception class

In order to use the first two solutions, we have to define an error handler method. You can read more about this topic in this article.

We can use @ResponseStatus with these error handler methods the same way we did with regular MVC methods in the previous section.

When we don’t need dynamic error responses, the most straightforward solution is the third one: marking the Exception class with @ResponseStatus:

@ResponseStatus(code = HttpStatus.BAD_REQUEST)
class CustomException extends RuntimeException {}

When Spring catches this Exception, it uses the settings we provided in @ResponseStatus.

Note, that when we mark an Exception class with @ResponseStatus, Spring always calls HttpServletResponse.sendError(), whether we set reason or not.

Also note, that Spring uses the same configuration for subclasses, unless we mark them with @ResponseStatus, too.

4. Conclusion

In this article, we saw how we can use @ResponseStatus to set HTTP response code in different scenarios, including error handling.

As usual, the examples are available over on GitHub.

Related posts:

Java Program to Implement Double Order Traversal of a Binary Tree
Java Program to Implement AttributeList API
Java toString() Method
Template Engines for Spring
Hướng dẫn Java Design Pattern – Composite
Spring Security OAuth2 – Simple Token Revocation
Java Program to Perform LU Decomposition of any Matrix
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Java Program to Implement Red Black Tree
Spring 5 WebClient
Java Program to Implement Aho-Corasick Algorithm for String Matching
A Quick Guide to Using Keycloak with Spring Boot
Custom Thread Pools In Java 8 Parallel Streams
Binary Numbers in Java
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Guide to the ConcurrentSkipListMap
Java Program to Implement Binary Search Tree
Multi Dimensional ArrayList in Java
Bootstrapping Hibernate 5 with Spring
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Working with Network Interfaces in Java
Inheritance with Jackson
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Functional Interface trong Java 8
Handling URL Encoded Form Data in Spring REST
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Spring Boot - Logging
Custom Error Pages with Spring MVC
Java Program to Implement Jarvis Algorithm
REST Web service: Basic Authentication trong Jersey 2.x
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Spring Boot - Google OAuth2 Sign-In