Last active
September 25, 2020 16:04
-
-
Save elegantcoder/b008af8c7b81c88ddf857f572ab739d4 to your computer and use it in GitHub Desktop.
[findFirstAnnotatedArgument] #java #springboot #@aspect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Aspect | |
public class ControllerJsonStyleAdvice { | |
private Object findFirstAnnotatedArgument(JoinPoint joinPoint, Class annotation) { | |
Object[] args = joinPoint.getArgs(); | |
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | |
Method method = signature.getMethod(); | |
Annotation[][] parameterAnnotations = method.getParameterAnnotations(); | |
for (int i = 0; i < parameterAnnotations.length; i++) { | |
for (int j = 0; j < parameterAnnotations[i].length; j++) { | |
if (annotation.isAssignableFrom(parameterAnnotations[i][j].getClass())) { | |
return args[i]; | |
} | |
} | |
} | |
return null; | |
} | |
@Before(value = "commonPointcut()") | |
public void setupParams(JoinPoint joinPoint) throws Throwable { | |
Object requestBodyArgument = findFirstAnnotatedParameter(joinPoint, RequestBody.class); | |
/* ... */ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment