Jetty:run fails with NoSuchMethodError with Spring 5

时光毁灭记忆、已成空白 提交于 2019-12-02 09:37:50

问题


I am trying to update my web application to current libraries. When I upgraded Spring from 3.1.1 to 5.0.0 it started to fail with:

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
    at org.springframework.web.servlet.FrameworkServlet.publishRequestHandledEvent(FrameworkServlet.java:1083)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1009)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)

I have found two relevant questions:

  • javax.servlet.http.HttpServletResponse.getStatus() not found
  • NoSuchMethodError while calling response.getStatus()

which explained a lot and I have upgraded jetty and servlet spec. But my app keeps failing.

Dependencies:

[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.7.5:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.5:compile
[INFO] +- ch.qos.logback:logback-core:jar:1.1.1:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.1:compile
[INFO] +- org.codehaus.janino:janino:jar:2.6.1:compile
[INFO] |  \- org.codehaus.janino:commons-compiler:jar:2.6.1:compile
[INFO] +- org.springframework:spring-web:jar:5.0.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.0.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-core:jar:5.0.0.RELEASE:compile
[INFO] |     \- org.springframework:spring-jcl:jar:5.0.0.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:5.0.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:5.0.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:5.0.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:5.0.0.RELEASE:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- com.google.code.gson:gson:jar:2.2.2:compile
[INFO] \- junit:junit:jar:4.10:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.1:test

Maven:

    <spring.framework.version>5.0.0.RELEASE</spring.framework.version>
    <maven-jetty-plugin.version>6.1.26</maven-jetty-plugin.version>
    <servlet.api.version>3.1.0</servlet.api.version>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.framework.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${servlet.api.version}</version>
        <scope>provided</scope>
    </dependency>

<build>
    <finalName>smartpos-backend-device</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <showWarnings>true</showWarnings>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>install</id>
                    <phase>install</phase>
                    <goals>
                        <goal>sources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>${maven-jetty-plugin.version}</version>

            <configuration>
                <jettyEnvXml>src/main/resources/jetty/jetty-env.xml</jettyEnvXml>
                <contextPath>smartpos-json</contextPath>
                <scanIntervalSeconds>0</scanIntervalSeconds>

                <connectors>
                    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                        <port>8080</port>
                    </connector>
                </connectors>
                <systemProperties>
                    <systemProperty>
                        <name>logback.configurationFile</name>
                        <value>logback.xml</value>
                    </systemProperty>
                    <systemProperty>
                        <name>LOG_PATH</name>
                        <value>.</value>
                    </systemProperty>
                </systemProperties>
            </configuration>
        </plugin>
    </plugins>
</build>

回答1:


Spring 5 requires a Servlet 3.1 container as a bare minimum however as you are using Jetty 6.1 which is (at most) a Servlet 2.5 container. So that is obviously not going to work.

You will have to upgrade your Jetty version. To upgrade instead of the maven-jetty-plugin you will need to use the appropriate jetty-maven-plugin (the name has changed) and it is part of Eclipse now.

<maven-jetty-plugin.version>9.4.7.v20170914</maven-jetty-plugin.version>

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${maven-jetty-plugin.version}</version>
</plugin>

This should use the latest version of both the plugin and thus Jetty.




回答2:


My bad I got it wrong. I think the problem is your jetty version:

https://mvnrepository.com/artifact/org.mortbay.jetty/jetty/6.1.26

Here the servlet API is 2.5 and when you add dependency in Maven and mark it with provided scope it means the required JARs will be provided on a later stage and not packed with the war. So the code compiles properly in the IDE and development but when you try to run it with jetty it uses the actual servlet-api coming from the jetty version. And for that method you need servlet-api version 3 or above



来源:https://stackoverflow.com/questions/46847578/jettyrun-fails-with-nosuchmethoderror-with-spring-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!