Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException in SpringBoot 2.1.2 with swagger 2.6.0 and consul 1.4.1 #15788

Closed
Hyerram opened this issue Jan 25, 2019 · 11 comments
Closed
Labels
status: invalid An issue that we don't feel is valid

Comments

@Hyerram
Copy link

Hyerram commented Jan 25, 2019

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 25, 2019
@wilkinsona
Copy link
Member

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.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Jan 25, 2019
@tuorao
Copy link

tuorao commented Feb 1, 2019

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/**");
}

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 1, 2019
@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Feb 1, 2019
@wilkinsona
Copy link
Member

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.

@wilkinsona wilkinsona added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Feb 1, 2019
@Hyerram
Copy link
Author

Hyerram commented Feb 1, 2019

Thanks @tuorao this works.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Feb 1, 2019
@wilkinsona
Copy link
Member

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.

@wilkinsona wilkinsona removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Feb 1, 2019
@tuorao
Copy link

tuorao commented Feb 2, 2019

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

@tuorao
Copy link

tuorao commented Feb 2, 2019

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

@wilkinsona wilkinsona reopened this Feb 4, 2019
@wilkinsona wilkinsona added the status: waiting-for-triage An issue we've not yet triaged label Feb 4, 2019
@wilkinsona
Copy link
Member

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 wilkinsona added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 4, 2019
@Hyerram
Copy link
Author

Hyerram commented Mar 27, 2019

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

@wilkinsona
Copy link
Member

@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.

@Hyerram
Copy link
Author

Hyerram commented Mar 27, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants