Package io.github.og4dev.annotation
Annotation Interface AutoResponse
Opt-in annotation to enable automatic API response wrapping for Spring REST controllers.
When this annotation is applied to a RestController
class or a specific request mapping method, the GlobalResponseWrapper
intercepts the returned object and automatically encapsulates it within the standardized
ApiResponse format.
Usage:
- Class Level (
ElementType.TYPE): Applies the wrapping behavior to all endpoint methods within the controller. - Method Level (
ElementType.METHOD): Applies the wrapping behavior only to the specific annotated method.
Example:
@RestController
@RequestMapping("/api/users")
@AutoResponse // All methods in this controller will be automatically wrapped
public class UserController {
* @GetMapping("/{id}")
public UserDto getUser(@PathVariable Long id) {
// Returns: { "status": "Success", "content": { "id": 1, ... }, "timestamp": "..." }
return userService.findById(id);
}
* @PostMapping
@ResponseStatus(HttpStatus.CREATED)
// @AutoResponse can also be placed here for method-level granularity instead of class-level
public UserDto createUser(@RequestBody UserDto dto) {
return userService.create(dto);
}
}
Exclusions:
To prevent errors and double-wrapping, the interceptor will safely ignore methods that return:
ApiResponseorResponseEntity(Assumes the developer has explicitly formatted the response)ProblemDetail(RFC 9457 error responses managed by the global exception handler)String(Bypassed to avoidClassCastExceptionwith Spring's internal string message converters)
- Since:
- 1.4.0
- Version:
- 1.4.0
- Author:
- Pasindu OG
- See Also: