Description
Affects: 5.3.1
Launching an application that uses Spring MVC 5.3.1 on a TomCat 8.5 gives the error:
Caused by: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;
at org.springframework.web.util.UrlPathHelper.skipServletPathDetermination(UrlPathHelper.java:265) ~[spring-web-5.3.1.jar:5.3.1]
at org.springframework.web.util.UrlPathHelper.getLookupPathForRequest(UrlPathHelper.java:250) ~[spring-web-5.3.1.jar:5.3.1]
at org.springframework.web.util.UrlPathHelper.resolveAndCacheLookupPath(UrlPathHelper.java:199) ~[spring-web-5.3.1.jar:5.3.1]
at org.springframework.web.servlet.handler.AbstractHandlerMapping.initLookupPath(AbstractHandlerMapping.java:567) ~[spring-webmvc-5.3.1.jar:5.3.1]
at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.getHandlerInternal(AbstractUrlHandlerMapping.java:136) ~[spring-webmvc-5.3.1.jar:5.3.1]
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:491) ~[spring-webmvc-5.3.1.jar:5.3.1]
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1255) [spring-webmvc-5.3.1.jar:5.3.1]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1037) [spring-webmvc-5.3.1.jar:5.3.1]
... 34 more
The application finds servlet-api version 4.0.1 in it's classpath (so isServlet4Present is true), but the HttpServletRequest passed to the method is actually from Tomcat's servlet 3, thus lacking the method getHttpServletMapping.
Activity
jhoeller commentedon Nov 18, 2020
I'll refine that check for 5.3.2: towards presence of the
HttpServletRequest.getHttpServletMapping
method rather than theHttpServletMapping
type. However, please also double-check the presence of Servlet API jars in your classpath; you shouldn't have that Servlet API 4.0.1 jar around in the first place when running on Tomcat 8.5.quaff commentedon Nov 19, 2020
Nice work, I have similar issue, I'm targeting servlet 4.0(Tomcat9) but I also provide embedded runtime using Jetty 9.4 which contains servlet 3.1 api, some tests is broken when I upgrade to spring 5.3.0.
jhoeller commentedon Nov 19, 2020
@FabioMarangonSMI @quaff it'd be great to verify that the latest 5.3.2 snapshot (https://repo.spring.io/snapshot/org/springframework/spring-framework-bom/) works for your scenarios. This is now as specific and as defensive as possible, so beyond this fix, the only thing to do about mismatches in the classpath is to enforce the presence of a single Servlet API jar.
quaff commentedon Nov 19, 2020
@jhoeller works fine for me now, thanks.
FabioMarangonSMI commentedon Nov 19, 2020
@jhoeller yeah, working here too, good job 👍