Implementing HTTP Redirects in Spring MVC @RestController

Dec 07, 2025 · Programming · 12 views · 7.8

Keywords: Spring MVC | @RestController | HTTP Redirect

Abstract: This article explores two primary methods for implementing HTTP redirects in Spring MVC @RestController. The first method involves injecting HttpServletResponse parameter and calling sendRedirect(), which is the most direct and widely accepted approach. The second method uses ResponseEntity to return redirect responses, avoiding direct dependency on Servlet API and providing a purer Spring implementation. The article analyzes the advantages, disadvantages, and use cases of both approaches, with code examples demonstrating practical implementations to help developers choose appropriate solutions based on project requirements.

HTTP Redirect Implementation in Spring MVC @RestController

In RESTful API development using Spring MVC framework, the @RestController annotation marks controller classes that return data rather than views. However, in certain business scenarios, depending on input parameters, there is a need to send HTTP redirect responses to clients. This article provides an in-depth examination of two main approaches for implementing redirects in @RestController, analyzing their technical details and applicability.

Method 1: Using HttpServletResponse for Redirects

This is the most direct and widely accepted approach for implementing redirects. By injecting HttpServletResponse parameter into controller methods, developers can directly call the sendRedirect() method provided by Servlet API.

The core implementation code is as follows:

@RestController
public class RedirectController {
    
    @RequestMapping("/api/redirect")
    public void handleRedirect(HttpServletResponse response) throws IOException {
        // Business logic evaluation
        boolean shouldRedirect = checkRedirectCondition();
        
        if (shouldRedirect) {
            response.sendRedirect("https://example.com/target");
        } else {
            // Normal request processing
            processNormalRequest();
        }
    }
    
    private boolean checkRedirectCondition() {
        // Redirect condition checking logic
        return true;
    }
    
    private void processNormalRequest() {
        // Normal request handling logic
    }
}

Advantages of this approach include:

However, this method also has limitations:

Method 2: Using ResponseEntity for Redirects

To reduce direct dependency on Servlet API, Spring provides a purer solution—using ResponseEntity to return redirect responses.

Implementation example:

@RestController
public class ResponseEntityRedirectController {
    
    @RequestMapping("/api/redirect-entity")
    public ResponseEntity<Void> handleRedirectWithEntity() {
        // Business logic evaluation
        if (shouldRedirect()) {
            HttpHeaders headers = new HttpHeaders();
            headers.setLocation(URI.create("https://example.com/target"));
            
            return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY);
        } else {
            // Return normal response
            return ResponseEntity.ok().body(null);
        }
    }
    
    @RequestMapping("/api/redirect-with-body")
    public ResponseEntity<String> handleRedirectWithResponseBody() {
        if (shouldRedirect()) {
            HttpHeaders headers = new HttpHeaders();
            headers.setLocation(URI.create("https://example.com/target"));
            
            return new ResponseEntity<>("Redirecting...", headers, HttpStatus.FOUND);
        } else {
            return ResponseEntity.ok("Normal response");
        }
    }
    
    private boolean shouldRedirect() {
        // Redirect condition evaluation
        return true;
    }
}

Advantages of using ResponseEntity include:

It's important to note that when a method always returns redirects, ResponseEntity<Void> should be used as the return type. If the method may return normal responses, appropriate generic types should be selected based on actual requirements.

Comparison and Selection Recommendations

In actual project development, the choice between these redirect implementation methods depends on specific requirements and project architecture:

When to choose HttpServletResponse:

When to choose ResponseEntity:

Best Practice Recommendations

Regardless of the implementation method chosen, the following best practices should be followed:

  1. Specify Redirect Type Clearly: Choose between permanent redirects (301) and temporary redirects (302/307) based on business requirements
  2. Validate Redirect URLs: Ensure redirect target URL validity and security to prevent open redirect vulnerabilities
  3. Appropriate Error Handling: Provide meaningful error responses when redirects fail
  4. Logging: Record redirect operations for monitoring and debugging
  5. Consider Cache Impact: Permanent redirects are cached by browsers—exercise caution when updating

By appropriately selecting and applying these redirect techniques, developers can flexibly handle various redirect requirements in Spring MVC @RestController while maintaining code clarity and maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.