Spring Log for Rest Controller
Asked Answered
J

3

6

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?

Jamnis answered 31/1, 2020 at 3:26 Comment(5)
Are you specifically trying to limit it to this class only, or is logging all of the calls okay? (Also note that even for "test" a class with this many methods is nuts; break them apart.)Felipa
I had answered a question to get details of the controller method using spring AOP hereLuminescent
Will it work on @Controller?Jamnis
@BunthaiDeng Yes , you need to modify the code accordingly. Your question is for RestController , right?Luminescent
it was just an additional question. Thank you, I understand.Jamnis
M
1

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());
    }

}
Mimosa answered 31/1, 2020 at 4:17 Comment(0)
Q
2

While you can use AOP tactics noted in the other answers to log the methods, there are better approaches if all you want to do is log all of the requests and responses for the endpoints those methods implement. There are a couple ways this can be done: Use Spring's CommonRequestLoggingFilter or write your own HandlerInterceptor. Here is a nice article on the first two options.

Quinate answered 31/1, 2020 at 20:7 Comment(0)
A
1

You can use Spring AOP:

package com.example;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class ExampleAspect {

    @Before("execution(* com.example.TestRestController(..)")
    public void log(JoinPoint joinPoint) {
        System.out.println("Executing :" + joinPoint.toString());
    }
}

Allethrin answered 31/1, 2020 at 3:34 Comment(0)
M
1

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());
    }

}
Mimosa answered 31/1, 2020 at 4:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.