Wiremock: WireMock Servlet API is not compatible with Spring Boot 2

Created on 7 Jan 2019  路  19Comments  路  Source: tomakehurst/wiremock

The Servlet API packaged with WireMock standalone is version 3.x whereas Tomcat container used by Spring Boot requires version 4.x. This causes an unresolvable conflict at runtime. Note that this problem cannot be solved by simply manipulating transitive dependencies because the Servlet API is part of the WireMock jar, not a dependency, and the same applies for Tomcat.

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;

I found this thread which discusses the same problem.

Most helpful comment

Ha, thanks for the quick answer @tomakehurst. I almost said the new wiremock-jre8 doesn't work because I was getting the same errors. But I noticed that this time I can see the artifact's dependencies so I tried to force the Servlet version via compile 'javax.servlet:javax.servlet-api:4.0.1' in Gradle. And guess what? It worked :-) All the tests are green now.

Thank you very much, WireMock is an amazing tool!

All 19 comments

@tomakehurst I'd like to help with this issue if you advice me how ... :-)

I shipped a new version yesterday that splits out Java 7 and Java 8+ JARs.

The Java 8+ JAR has the latest Jetty version (and therefore servlet API) so this might already be fixed.

Suggest you try the wiremock-jre8 artifact in your dependencies and see how you get on.

Ha, thanks for the quick answer @tomakehurst. I almost said the new wiremock-jre8 doesn't work because I was getting the same errors. But I noticed that this time I can see the artifact's dependencies so I tried to force the Servlet version via compile 'javax.servlet:javax.servlet-api:4.0.1' in Gradle. And guess what? It worked :-) All the tests are green now.

Thank you very much, WireMock is an amazing tool!

Thanks for sharing, useful to know you had to bump the version slightly higher.

I think the whole point of this ticket is for WM to natively support Spring Boot 2. Forcing the servlet version doesn't seem like that, because there's no guarantee that whatever version the user chooses would be compatible with what WM is expecting. If it works today, that's a fluke; it may not work tomorrow with another servlet version, say 4.1.0 (hypothetical).

Is wiremock-jre8 compatible with Tomcat or it is compatible only with Jetty, I am using Tomcat and I am still getting the same exception
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;

@jevgenimarenkov which version of the Servlet API do you have on your classpath?

@tomakehurst I am using Tomcat 9.0.16 and Servlet 4.

If it's possible to drop down to a Tomcat version that supports servlet 3.1.0 that would help

I'm getting the same error with Spring Boot 2.1.3 and Wiremock (standalone) 2.22.0.

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;
    at org.apache.catalina.core.ApplicationHttpRequest.setRequest(ApplicationHttpRequest.java:690) ~[tomcat-embed-core-9.0.16.jar:9.0.16]

Spring Boot 2.0.8, which uses tomcat-embedded-core-8.5.37, doesn't cause this exception.

I would be nice if WireMock would work out of the box with newer Spring Boot versions.

I'm getting the same error with Spring Boot 2.1.3 and Wiremock (standalone) 2.22.0.

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;
  at org.apache.catalina.core.ApplicationHttpRequest.setRequest(ApplicationHttpRequest.java:690) ~[tomcat-embed-core-9.0.16.jar:9.0.16]

Spring Boot 2.0.8, which uses tomcat-embedded-core-8.5.37, doesn't cause this exception.

I would be nice if WireMock would work out of the box with newer Spring Boot versions.

Your suggestion worked for me. It has bothered me for a long time. Thanks!

Any idea why wiremock overrides HttpServletRequest classes?

@michalborek WireMock embeds Jetty, which brings in a specific version of the Servlet API (3.x.x at present).

Unfortunately each major version upgrade of the Servlet API seems to bring breaking changes with it, so if you're trying to build a Servlet 4.0.0 based app and have WireMock on the classpath, then the 4.0.0 version will take precedence and break WireMock.

I'm trying figure out a way to avoid this, but when I've tried shading the Servlet API in the past it's prevented Jetty from starting.

Any progress here? There seems to be an interaction with the jdk used - with jdk8 we can build/run our tests - with jdk11 its the good old 'java.lang.NoSuchMethodError' from above. (Using Spring-cloud-contract-wiremock 2.2.1.

Seems like jetty is not in a hurry of supporting Servlet Api 4 .... jetty-issue-4578

When using Spring boot with wiremock jre8 standalone and deploy in azure..Facing below No Server ALPNProcessors...Any suggestions?
Caused by: java.lang.IllegalStateException: No Server ALPNProcessors!at wiremock.org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory.(ALPNServerConnectionFactory.java:52) ~[wiremock-jre8-standalone-2.26.3.jar!

User the server's default jars other than javax.servlet:javax.servlet-api:4.0.1. For my case, it's tomcat 9.0.30 lib/. It doesn't need the javax.servlet:javax.servlet-api: XXXXXX.

@kbking01

When using Spring boot with wiremock jre8 standalone and deploy in azure..Facing below No Server ALPNProcessors...Any suggestions?
Caused by: java.lang.IllegalStateException: No Server ALPNProcessors!at wiremock.org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory.(ALPNServerConnectionFactory.java:52) ~[wiremock-jre8-standalone-2.26.3.jar!

I think this is a separate issue:
https://github.com/eclipse/jetty.project/issues/1894
https://github.com/jetty-project/jetty-alpn

probably using a newer version of openjdk will fix that for you:
use >= 8u252-jdk-slim tag when building an app if you execute tests with WireMock during builds

I'm closing this as the standalone JAR now shades the version of servlet-api that it uses.

Was this page helpful?
0 / 5 - 0 ratings