【问题标题】:NoSuchMethodError in servlet contextservlet 上下文中的 NoSuchMethodError
【发布时间】:2014-07-04 10:02:48
【问题描述】:

我有两个 maven 模块:commonwebapp

他们有相同的父项目,webapp 依赖于common

common 模块中有一个类MapServer

public class MapServer {
    public TileResponse getTileResponse(Coord coordinate, int size, String format, String[] layers) {
        ....
        return null;
    }
}

我可以这样运行:

public static void main(....){
    ....
    TileResponse tileResponse = mapServer.getTileResponse(coord, 256, "png", new String[]{"layer"});
}

它成功了(得到了预期的结果,没有任何错误)。

然而,一旦我通过 servlet 运行 webapp 模块,

public class ServeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
            ......
            tileResponse = mapServer.getTileResponse(coord, 256,format , layers.split(","));
    }

}

我得到了错误:

July 04, 2014 5:55:36 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [tile] in context with path [/test] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoSuchMethodError: MapServer.getTileResponse(Lcore/Coord;ILjava/lang/String;[Ljava/lang/String;)LTileResponse;

发生了什么事?

我正在使用tomcat maven plugin

<build>
    <finalName>webapp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>8080</port>
                <path>/test</path>
                <uriEncoding>UTF-8</uriEncoding>
                <finalName>gtwebapp</finalName>
                <server>tomcat7</server>
            </configuration>
        </plugin>
    </plugins>
</build>

【问题讨论】:

    标签: java maven servlets


    【解决方案1】:

    java.lang.NoSuchMethodError 表示用于构建调用代码的类(在您的情况下为 MapServer)的版本与运行时可用的版本不同。

    一旦您对 MapServer 的工作版本感到满意:

    • 重建通用项目并将其安装到本地 maven 存储库中
    • 针对此版本的通用项目重新构建 webapp
    • 在重试之前在 tomcat 上重新部署 webapp

    【讨论】:

    • @hguser 如果您对公共模块进行了更改,除非您全部重建它们,否则 webapp 模块不会自动获取它。你需要小心 maven,你的 webapp 不会选择旧版本的公共模块。这就是我概述上述步骤的原因。
    • 您的意思是即使我更改了公共模块中的代码,但 webapp 模块仍可能使用我本地存储库中的旧版本 jar?
    • @hguser 如果您不重新构建您的公共模块并使其可用于本地存储库中的 webapp,那么是的,webapp 将继续使用本地存储库中的现有(旧)jar。
    • 谢谢,看来你是对的,在我重建公共模块后它可以工作了。
    猜你喜欢
    • 2015-11-24
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 2014-07-21
    • 2016-12-04
    • 2011-04-16
    • 2011-04-14
    • 2010-12-21
    相关资源
    最近更新 更多