The parameter name of the springboot method

There are two ways to get the parameter name of the method in springboot

The method used to verify, you can see that there are two parameter names, one test and one num

public String getString(String test,Integer num) { return ""; }

1. Use spring's LocalVariableTableParameterNameDiscoverer

Define the use class

private static LocalVariableTableParameterNameDiscoverer parameterNameDiscovere = new LocalVariableTableParameterNameDiscoverer();

I use it here in aop, so I use the ProceedingJoinPoint get method, parameterNameDiscovere.getParameterNames() needs to pass in the method

String[] parameterNames = parameterNameDiscovere.getParameterNames(getMethod((ProceedingJoinPoint) joinPoint));
for (String parameterName : parameterNames) {
    logger.info(parameterName);
}

The output result is that the parameter names are output correctly in order

test

on one

If the method has no parameters, parameterNames is returned as null

2. ProceedingJoinPoint when using AOP

  Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        String[] strings = methodSignature.getParameterNames();
        System.out.println(Arrays.toString(strings));

Output

[Test whether]

 

At present, I have summarized two more convenient methods, and if there are good ones, you can comment to me.

Guess you like

Origin blog.csdn.net/Mint6/article/details/94991183