Logging the Whole Rest Controller Class in Spring

Bunthai Deng :

I have a rest controller contains many methods

@RestController
@RequestMapping("v1/test")
public class TestRestController {

   ...... 100 methods (GET, POST, PATCH, etc)
}

How can I know which method is being accessed without using print in each method?
Are there any ways to do that?

Anuradha :

Using Aspect Oriented Programming with Spring:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;    

@Aspect
public class SpringAspect {

    private static final Logger LOG = LoggerFactory.getLogger(SpringAspect.class);

    @Before("execution(* sample.package.path.TestRestController.*(..))")
    public void executedMethodsLogger(JoinPoint joinPoint) {
        LOG.info("[ Executed method {} ]", joinPoint.toString());
    }

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=6030&siteId=1