Amir_Af
Amir_Af

Reputation: 574

Adding @Around advice to @RestController methods

Is it possible to add @Around advice to @RestController ?

I tried anything but with no success.

Below is my simple logging aspect

@Aspect
public class LoggingAspect {

    @Around("execution(public * zbc.sxw.rest.*.*(..))")
    public void logRestCall(ProceedingJoinPoint joinPoint) throws Throwable 
    {
        System.out.println("Write code for before advise");
        joinPoint.proceed();
        System.out.println("Write code for after advise");
    }
}

The classes under the rest package are all annotated with @RestController annotation.

If I change the .rest. to .ctrl. which include @Controller annotated classes the Aspect is working fine.

Any idea what is the problem with @RestController ?

Thanks.

Upvotes: 2

Views: 690

Answers (1)

harper
harper

Reputation: 11

try add @Componet annotation on your aspect class e.g.

@Component
@Aspect
public class LoggingAspect {

    @Around("execution(public * zbc.sxw.rest.*.*(..))")
    public void logRestCall(ProceedingJoinPoint joinPoint) throws Throwable 
    {
        System.out.println("Write code for before advise");
        joinPoint.proceed();
        System.out.println("Write code for after advise");
    }
}

Upvotes: 1

Related Questions