Repository navigation
Description
Problem
Right now, developers must manually wrap success data payloads in ResponseEntity<UnravelDocsResponse<T>> and call ResponseBuilderService.buildUserResponse(...) inside almost every single controller method. This leads to duplicate boilerplate code across dozens of controllers. If a developer forgets to apply the builder, that endpoint returns raw data, violating the API envelope contract.
Proposed Solution
Create a class implementing Spring's ResponseBodyAdvice<Object>. It should intercept response bodies before they are written. If a controller returns raw data (e.g. List<UserDto>), the advice automatically wraps it in a standard ApiResponse<List<UserDto>> with a 200 OK status code. This allows controllers to return raw DTOs or entities directly, reducing boilerplate.
User Stories
- As a developer, I want to return my DTOs directly from controller methods without calling wrapper builders so that the controller layer remains clean and easy to read.
Acceptance Criteria
-
GlobalResponseBodyAdviceis created undersrc/main/java/com/xtraction/api/shared/response/GlobalResponseBodyAdvice.java. - Controller methods returning custom objects, lists, or primitive types are automatically wrapped in
ApiResponse<T>. - Response status codes, timestamps, paths, and trace IDs are dynamically added by the advice.
- Exclusions are handled for static resources, files, Swagger documents, and endpoints annotated with custom bypass annotations.
Proposed Technical Details
package com.xtraction.api.shared.response;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import java.time.Instant;
@RestControllerAdvice(basePackages = "com.xtraction.api")
public class GlobalResponseBodyAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
// Prevent double-wrapping or wrapping problem details, swagger, etc.
Class<?> type = returnType.getParameterType();
return !type.equals(ApiResponse.class) && !type.equals(Void.class);
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response) {
int status = 200;
if (response instanceof ServletServerHttpResponse servletResponse) {
status = servletResponse.getServletResponse().getStatus();
}
String path = request.getURI().getPath();
return ApiResponse.builder()
.success(status >= 200 && status < 300)
.statusCode(status)
.message("Operation completed successfully")
.data(body)
.timestamp(Instant.now())
.path(path)
.build();
}
}Tasks
- Create
GlobalResponseBodyAdvice.java. - Configure exclusions for swagger, actuator, and custom files.
- Refactor several controllers to test the automatic wrapping logic.
- Remove manual
responseBuilderService.buildUserResponse(...)calls once automatic wrapping is verified.
Activity