How to Design Elegant Backend APIs with Automatic Response Wrapping in Spring
This article demonstrates how to design elegant backend API responses using a standardized JSON format with categorized error codes, then implements automatic response wrapping in Spring Boot via a custom @ResponseResult annotation, ResponseBodyAdvice, and ControllerAdvice to eliminate boilerplate code while preserving clean controller logic.
Reading font

In modern microservice architectures with frontend-backend separation, backend APIs typically return JSON responses. The author defines a standard response structure with three fields: code (integer status code), message (string description), and data (object payload).
Error Code Categorization
Instead of adding status codes ad-hoc during development (e.g., 101 for permission error, 102 for parameter error), the author recommends grouping errors into numeric ranges inspired by HTTP status codes: 1000–1999: parameter errors 2000–2999: user errors 3000–3999: interface exceptions
This lets frontend developers quickly identify the error category from the code alone, then use the message for details.
Message and Data Design
The message field pairs with each status code, ideally defined in an enum for maintainability. The data field holds business-specific JSON. A generic Result<T> wrapper class encapsulates all three fields.
Evolution of Controller Implementation
1. Manual Wrapping
Initially, controllers manually instantiate Result objects:
@GetMapping("/order/{id}")
public Result<Order> getOrder(@PathVariable Long id) {
Order order = orderService.getOrder(id);
return new Result<>(200, "success", order);
}2. Static Factory Methods
Adding static methods success(T data) and failure(int code, String msg) to Result simplifies calls to Result.success(order).
3. Annotation-Driven Automatic Wrapping (Core Solution)
To avoid repeating Result.success() and keep controllers returning plain domain objects, the author introduces:
@ResponseResult annotation – marks methods/classes whose return values need wrapping.
HandlerInterceptor – inspects the handler method for @ResponseResult and stores a flag in the request attributes.
ResponseBodyAdvice + @ControllerAdvice – implements supports() to check the request flag and beforeBodyWrite() to wrap the return value into Result if needed. Exception handling is done by detecting if the body is an exception type.
After this setup, controllers become clean:
@ResponseResult
@GetMapping("/order/{id}")
public Order getOrder(@PathVariable Long id) {
return orderService.getOrder(id); // returns plain Order
}Exception Handling
The beforeBodyWrite method checks body instanceof Throwable to wrap exceptions into the same Result structure. Global exception handling is mentioned as a separate concern.
Performance Consideration
The interceptor uses reflection to read the annotation on every request. The author suggests caching the annotation presence (e.g., in a ConcurrentHashMap keyed by method) to avoid repeated reflection.
Summary
The article walks through a complete design process: standardizing the response format, categorizing error codes, then progressively refactoring from manual wrapping to a declarative, annotation-driven solution using Spring's ResponseBodyAdvice and ControllerAdvice. The final approach keeps business logic free of framework concerns while guaranteeing consistent JSON output.
About this article
For attribution, corrections or content rights, contact