Spring-boot: ClassCastException in SpringBoot 2.1.2 with swagger 2.6.0 and consul 1.4.1

Created on 25 Jan 2019  路  11Comments  路  Source: spring-projects/spring-boot

2019-01-25 10:12:14,886 ERROR [o.a.c.c.C.[.[.[.[dispatcherServlet]] [http-nio-8200-exec-2] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.web.servlet.resource.ResourceHttpRequestHandler cannot be cast to org.springframework.web.method.HandlerMethod] with root cause
java.lang.ClassCastException: org.springframework.web.servlet.resource.ResourceHttpRequestHandler cannot be cast to org.springframework.web.method.HandlerMethod
    at com.Something.special.service.interceptor.SecurityInterceptor.preHandle(SecurityInterceptor.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:246)
    at org.springframework.cloud.context.scope.GenericScope$LockedScopedProxyFactoryBean.invoke(GenericScope.java:494)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.something.special.service.interceptor.SecurityInterceptor$$EnhancerBySpringCGLIB$$6f55b6e8.preHandle(<generated>)
    at org.springframework.web.servlet.HandlerExecutionChain.applyPreHandle(HandlerExecutionChain.java:136)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1033)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117)
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

Upgrading from spring boot 1.5.x to 2.1.2. Creating ClassCastException while checking the swagger ui, throws ClassCastException, Actual behavior Interceptors processing static resource requests, so manually excluded the static resources while registering. Appreciated if someone resolves without excluding manually.

invalid

Most helpful comment

Perhaps you are using the interceptor provided by the spring boot.
If you use a custom annotation that extends the HandlerInterceptorAdapter,
When you change to spring boot 1.5 -> 2.1, do this

@Override
public void addInterceptors(InterceptorRegistry registry) {
  registry.addInterceptor(routingSessionInterceptor())
    .addPathPatterns("/**")
    .excludePathPatterns("/swagger**", "/webjars/springfox-swagger-ui/**");
}

All 11 comments

This doesn't look like a problem in Spring Boot to me. SecurityInterceptor.preHandle(SecurityInterceptor.java:77) is attempting to cast an instance of org.springframework.web.servlet.resource.ResourceHttpRequestHandler to org.springframework.web.method.HandlerMethod which fails as ResourceHttpRequestHandler is not a HandlerMethod. That's always been the case so it's not clear to me why the behaviour has changed. It's also not clear to me where Swagger fits in as I can't see any sign of it in the stack trace.

If you would like us to spend any more time looking at this, please take the time to explain why you believe it's a bug in Spring Boot and to provide a minimal and complete example (as a zip attached to this issue or in a Git repository) that works with Spring Boot 1.5 and fails with 2.1.

Perhaps you are using the interceptor provided by the spring boot.
If you use a custom annotation that extends the HandlerInterceptorAdapter,
When you change to spring boot 1.5 -> 2.1, do this

@Override
public void addInterceptors(InterceptorRegistry registry) {
  registry.addInterceptor(routingSessionInterceptor())
    .addPathPatterns("/**")
    .excludePathPatterns("/swagger**", "/webjars/springfox-swagger-ui/**");
}

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Thanks @tuorao this works.

I'm not that I understand why the above works, but I'm glad that it does. In the absence of a sample that we could look at, we won't be able to propose a better solution if one should exist so I'm going to close this one. We can re-open it in the future if the requested sample is provided.

I'll make a sample of this problem.
wait plz :D @wilkinsona

https://github.com/tuorao/sample-code

After downloading and executing this project,
Try the url

http://localhost:8080/swagger-ui.html

and check addInterceptor method

@wilkinsona

Thanks for the sample. Due to an enhancement in Spring Framework, interceptors are now called when accessing static resources. As such, you cannot assume that every handler that's passed into your interceptor will be a HandlerMethod. You should update your interceptor to be more defensive and only cast handler once you've done an appropriate instanceof check.

@wilkinsona Is this issue classCastException fixed on 2.1.3.RELEASE ? When i call the /health endpoint it still throws the ClassCastException.

@Hyerram No. There was nothing to fix in Spring Boot. The bug was in application code in an interceptor that was making a faulty assumption that the handler would always be a HandlerMethod.

Found that we need to call /actuator/health instead of /health in all spring 2.X upgrades.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

igormukhin picture igormukhin  路  55Comments

masc3d picture masc3d  路  33Comments

ganeshkrishnan1 picture ganeshkrishnan1  路  63Comments

LutzStrobel picture LutzStrobel  路  100Comments

dsyer picture dsyer  路  40Comments