What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If you want to change the JSON or other body returned by a Spring MVC REST endpoint, use Spring MVC’s ResponseBodyAdvice. If you want cross-cutting logic around a Spring bean method—such as inspecting its normal return value—use Spring AOP. In particular, @AfterReturning can observe a result but cannot replace it; use @Around when the method’s caller-visible result must change.

These mechanisms act at different stages: an aspect works around a proxied Java method execution, while ResponseBodyAdvice can customize an MVC response body before an HTTP message converter writes it. The guidance below is for Spring MVC; it does not cover incoming-request filters or WebFlux response handling.

Choose the hook for the stage you need to change

Need Use What it affects
Inspect a method’s successful return @AfterReturning Observes a normally returned value; does not substitute a different value for the caller.
Replace or wrap a method result @Around Controls the value returned from the advised method execution.
Customize an MVC response body before serialization ResponseBodyAdvice Changes the body before the selected HttpMessageConverter writes it.
Intercept self-invocation, constructors, or field access Native AspectJ weaving, if appropriate Can cover join points outside Spring AOP’s proxy-based method-execution scope, with weaving configured.

A controller’s Java return object is not the same thing as the serialized HTTP response. An aspect that changes a method result can affect later MVC processing, but it does not by itself give you direct control over serialized bytes, status codes, headers, content negotiation, or every response type. For body customization at the MVC conversion stage, use ResponseBodyAdvice.

Inspect a successful return with @AfterReturning

Bind the returned value with the advice annotation’s returning attribute. The name must match the advice parameter. This advice runs only when the target method completes normally; it is suitable for auditing or observation, not for replacing the result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@Aspect
@Component
class ReturnAuditAspect {
    @AfterReturning(
        pointcut = "execution(* com.example.api..*(..))",
        returning = "result")
    void inspect(Object result) {
        // Record or inspect the normally returned value.
    }
}

This is a schematic example: adapt the package expression and register the aspect in the application’s Spring AOP configuration. Its pointcut must match methods on beans that Spring actually proxies. Spring’s documentation explicitly notes that after-returning advice cannot return a totally different reference: Spring advice semantics.

If the method throws, @AfterReturning does not run. Use @AfterThrowing to handle an exceptional method completion, or use an MVC exception-handling mechanism when the concern is rendering an HTTP error response. @After is finally-style advice for normal or exceptional completion and is appropriate for cleanup, not return-value replacement. Spring notes that @AfterThrowing is not a general callback for exceptions thrown by other advice.

Transform a method result with @Around

Use around advice only when the method-level result itself should change. Call proceed() to execute the target method, transform or wrap its result, and return the new value to the caller.

@Aspect
@Component
class ResultWrappingAspect {
    @Around("execution(* com.example.api..*(..))")
    Object wrap(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        return transform(result);
    }

    private Object transform(Object result) {
        // Apply a deliberate, endpoint-compatible transformation.
        return result;
    }
}

This schematic example is not tested code. A transformation changes the Java value before downstream MVC processing. Preserve the endpoint’s declared type and serialization expectations, and design deliberately before applying a wrapper to error responses, streaming results, or already-specialized response types. If the requirement is instead to modify an MVC body immediately before conversion, implement ResponseBodyAdvice.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Modify an MVC response body with ResponseBodyAdvice

Spring MVC’s ResponseBodyAdvice is designed to inspect or modify a body returned by an @ResponseBody or ResponseEntity controller method after the handler executes and before the selected HttpMessageConverter writes the body. Implement supports to choose applicable responses and beforeBodyWrite for the customization. See the Spring MVC controller advice documentation.

Register the advice through MVC handler infrastructure or controller advice. Apply it selectively—for example, by controller, annotation, return type, or media type—so unrelated endpoints retain their existing representations. @RestControllerAdvice combines @ControllerAdvice with @ResponseBody; exception-handler methods declared there also render their results in the response body.

Understand Spring AOP’s proxy and pointcut limits

Spring AOP advises method-execution join points on Spring beans. It uses JDK dynamic proxies or CGLIB proxies, so an advised call must pass through the relevant proxy. When a target object calls one of its own methods directly, that self-invocation bypasses the proxy and the inner method is not intercepted. With JDK proxies, only interface methods exposed by the proxy are available; CGLIB’s subclass proxies also have proxying constraints. Consult the Spring proxying documentation for the constraints relevant to your configuration.

Spring AOP supports pointcut designators including execution, within, this, target, args, annotation-based designators, and Spring’s bean designator. It does not support the full AspectJ join-point model: designators such as call, get, set, and constructor-related join points are not accepted in Spring AOP pointcuts. Keep pointcuts narrow by constraining them to the relevant package, annotation, or bean, and use named pointcuts when they improve clarity. Details are in Spring’s pointcut reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

AspectJ-style syntax is not the same as AspectJ weaving

Using @Aspect annotations with Spring’s auto-proxy support does not, by itself, enable the AspectJ weaver. Spring still creates AOP proxies. Native AspectJ compile-time or load-time weaving is a separate option for requirements such as intercepting internal calls, constructor execution, or field access. It requires configuring the appropriate weaving process; the Spring documentation on using AspectJ describes this distinction.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the endpoint contract before applying a transformation

Before changing a method result or response body, determine which parts of the HTTP contract must remain intact. A transformation that is safe for an ordinary object response may be wrong for another endpoint shape.

  • Return type: Keep the transformed value compatible with the controller method and the downstream serialization path.
  • Status and headers: Decide whether they must remain unchanged; changing a body alone is not a general mechanism for changing response metadata.
  • Content negotiation: Ensure the new body remains appropriate for the negotiated media type and selected converter.
  • Special response cases: Review error, streaming, and already-specialized responses individually rather than applying a blanket wrapper.
  • Interception path: Verify the call goes through a Spring proxy if using Spring AOP; use a deliberately configured weaving approach if the required join point is outside proxy-based AOP.

The cited Spring reference pages displayed Spring Framework 7.0.9 and 6.2.19 as stable when consulted on September 30, 2026. The title does not specify a target version, so check the APIs and configuration for the Spring version used by your application.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.