Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Customizing Block Exception Responses in Sentinel 1.8.0 for Spring MVC

Tech May 8 3

In Sentinel 1.8.0 and later, the legacy WebCallbackManager and UrlBlockHandler interfaces have been phased out. Developers integrating Sentinel with Spring Boot must adopt the BlockExceptionHandler interface to define custom HTTP responses for traffic-rejected requests.

Implementing a Custom Handler

Register a Spring-managed component that implements BlockExceptionHandler. The framework automatically detects the bean and injects it into the request interception pipeline.

@Component
public class RateLimitResponseHandler implements BlockExceptionHandler {
    private final ObjectMapper jsonMapper = new ObjectMapper();

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException exception) throws Exception {
        response.setStatus(429);
        response.setContentType("application/json; charset=utf-8");

        Map<String, Object> errorPayload = new LinkedHashMap<>();
        errorPayload.put("code", "RATE_LIMIT_EXCEEDED");
        errorPayload.put("detail", "Traffic threshold breached. Retry after a cooldown period.");
        errorPayload.put("traceId", System.currentTimeMillis());

        jsonMapper.writeValue(response.getWriter(), errorPayload);
    }
}

The resulting HTTP response follows this structure:

{"code":"RATE_LIMIT_EXCEEDED","detail":"Traffic threshold breached. Retry after a cooldown period.","traceId":1715203456789}

Common Configuration Pitfalls

Attempting to register a custom handler via WebMvcConfigurer.addInterceptors() or by direct assigning a property on SentinelWebMvcConfig will fail. The adapter configuration extends BaseWebMvcConfig, which deliberately omits a public setter for block handlers in this release. The interceptor chain relies on the Spring application context to resolve the BlockExceptionHandler bean automatically.

For reliable integration, rely on the auto-configuration provided by the sentinel-spring-webmvc-adapter dependency rather than manual interceptor registration or reflection-based field injection.

Tags: sentinel

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.