Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get method information at Interceptor preHandle method in spring boot 1.3

Tags:

spring-boot

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug(">>> handler: " + handler);
    }

    HandlerMethod handlerMethod = (HandlerMethod) handler;
    Login login = handlerMethod.getMethod().getAnnotation(Login.class);
}

I had above interceptor code in spring 3.X that works. I like to use this code in the Controller having @CrossOrigin and @RequestMapping method at spring boot 1.3. but below error is occured.

How to get method information at Interceptor preHandle method in spring boot 1.3?

Caused by: java.lang.ClassCastException: org.springframework.web.servlet.handler.AbstractHandlerMapping$PreFlightHandler cannot be cast to org.springframework.web.method.HandlerMethod
like image 744
uuidcode Avatar asked Feb 03 '16 03:02

uuidcode


1 Answers

Is added to a portion to process the request CORS added after 4.2 Spring this will again process the "interceptor". So you can add code to check whether the "handler" of the object type "HandlerMethod" type.

e.g.

if (handler instanceof HandlerMethod) {...}
like image 123
sbcoba Avatar answered Nov 15 '22 08:11

sbcoba