使用 Maven、Tomcat 7 和 Websocket 构建可执行的 war/jar



我正在尝试使用 Maven 和 Tomcat 7 构建一个可执行的 WAR/JAR。该项目包含 Websocket 和 REST API。

为此,我在pom.xml文件中添加了以下插件:

    <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>tomcat-run</id>
                    <goals>
                        <goal>exec-war-only</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <path>/TransSafeGWService</path>
                        <enableNaming>false</enableNaming>
                        <finalName>standalone.jar</finalName>
                        <charset>utf-8</charset>
                    </configuration>
                </execution>
            </executions>

之后,由于 REST 服务可以工作,但 Websocket 服务不能工作,所以我向插件添加了其他依赖项:

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>tomcat-run</id>
                    <goals>
                        <goal>exec-war-only</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <path>/TransSafeGWService</path>
                        <enableNaming>false</enableNaming>
                        <finalName>standalone.jar</finalName>
                        <charset>utf-8</charset>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat7-websocket</artifactId>
                    <version>7.0.67</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-util</artifactId>
                    <version>7.0.67</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-coyote</artifactId>
                    <version>7.0.67</version>
                </dependency>
            </dependencies>
        </plugin>

使用该依赖项,我收到此类错误:

java.lang.NoSuchMethodError: org.apache.catalina.connector.RequestFacade.upgrade(Ljava/lang/Class;)Lorg/apache/coyote/http11/upgrade/servlet31/HttpUpgradeHandler;
    at org.apache.tomcat.websocket.server.UpgradeUtil.doUpgrade(UpgradeUtil.java:238)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:77)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

在这种情况下,可能有什么问题?

在完整的pom.xml文件下面:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ti.white</groupId>
<artifactId>TransSafeGWService</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TransSafeGWService</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.18</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.3.7</version>
    </dependency>
    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>

</dependencies>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webXml>WebContentWEB-INFweb.xml</webXml>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>tomcat-run</id>
                    <goals>
                        <goal>exec-war-only</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <path>/TransSafeGWService</path>
                        <enableNaming>false</enableNaming>
                        <finalName>standalone.jar</finalName>
                        <charset>utf-8</charset>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat7-websocket</artifactId>
                    <version>7.0.67</version>
                </dependency>

                <dependency>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-util</artifactId>
                    <version>7.0.67</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-coyote</artifactId>
                    <version>7.0.67</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

根据要求,以下是 web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>TransSafeGWService</display-name>
  <servlet>
    <servlet-name>Data Acquisition Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.ti.transafe.acquisitionService</param-value>
    </init-param>
    <init-param>
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Data Acquisition Service</servlet-name>
    <url-pattern>/Service/* </url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
我认为

您的问题可能与错误版本的servlet api有关。Tomcat 7 支持 3.0 及更低版本,但您似乎使用的是 servlet 3.1 版本。

您的异常显示找不到此方法,这只是 servlet 3.1 或更高版本的一部分。

Since: 
 Servlet 3.1

即使您没有在问题中包含 web.xml,堆栈跟踪也会导致此分辨率

请参阅您的网站.xml有此声明。

  <web-app version="3.0"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
    http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

更新:

您正在使用 tomcat7-maven-plugin 的 2.1 版,该插件使用 tomcat 版本 7.0.37。然后在依赖项中,您尝试使用具有不同版本 7.0.67 的 tomcat jar。必须对两者使用相同的版本。

您有两种可能性:

  1. 将所有 jar 的版本设置为 7.0.37

  2. 您可以使用 tomcat7-maven-plugin 的最新版本 2.2,并使用该属性来设置 tomcat 版本。我会更喜欢这个。也许您也可以使用 2.1 设置此属性。您可以试试吧。

相关内容

  • 没有找到相关文章

最新更新