how to get controller method name in Spring Interc

2020-04-11 07:35发布

In my application based on spring mvc and spring security I am using @Controller annotation to configure controller.

I have configured Spring Handler Interceptor and in preHandle() method , I want to get method name which is going to be call by interceptor.

I want to get custom annotation defined on controller method in preHandle() method of HandlerInterceptor so that I can manage by logging activity for that particular method.

Please have a look at my application requirement and code

@Controller
public class ConsoleUserManagementController{
@RequestMapping(value = CONSOLE_NAMESPACE + "/account/changePassword.do", method = RequestMethod.GET)
@doLog(true)
public ModelAndView showChangePasswordPage() {
    String returnView = USERMANAGEMENT_NAMESPACE + "/account/ChangePassword";
    ModelAndView mavChangePassword = new ModelAndView(returnView);
    LogUtils.logInfo("Getting Change Password service prerequisit attributes");
    mavChangePassword.getModelMap().put("passwordModel", new PasswordModel());
    return mavChangePassword;
}
}

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
   // here I want the controller method name(i.e showChangePasswordPage() 
   // for /account/changePassword.do url ) to be called and that method annotation 
   // (i.e doLog() ) so that by viewing annotation , I can manage whether for that 
   // particular controller method, whether to enable logging or not.
}

I am using SPRING 3.0 in my application

1条回答
Evening l夕情丶
2楼-- · 2020-04-11 08:38

Don't know about the Handler interceptor, but you could try to use Aspects and create a general interceptor for all your controller methods.

Using aspects, it would be easy to access your joinpoint method name.

You can inject the request object inside your aspect or use:

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

To retrieve it from your advice method.

For instance:

@Around("execution (* com.yourpackages.controllers.*.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object doSomething(ProceedingJoinPoint pjp){
 pjp.getSignature().getDeclaringType().getName();
}
查看更多
登录 后发表回答