how to get controller method name in Spring Interceptor preHandle method

人盡茶涼 提交于 2019-12-31 01:53:07

问题


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:


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


来源:https://stackoverflow.com/questions/8384903/how-to-get-controller-method-name-in-spring-interceptor-prehandle-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!