uuidcode
uuidcode

Reputation: 3970

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

@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

Upvotes: 6

Views: 4641

Answers (1)

sbcoba
sbcoba

Reputation: 91

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) {...}

Upvotes: 6

Related Questions