简体   繁体   English

使用Maven,Tomcat 7和Websocket构建可执行的War / Jar

[英]Build an Executable War/Jar with Maven, Tomcat 7 and Websocket

I'm trying to build an executable WAR/JAR using Maven and Tomcat 7. The Project contains Websocket and REST API. 我正在尝试使用Maven和Tomcat 7构建可执行的WAR / JAR。该项目包含Websocket和REST API。

In order to do that, I added the following plugin to the pom.xml file: 为了做到这一点,我在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>

After that, since REST Service was working but Websocket Service was not, I added other dependencies to the plugin: 之后,由于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>

Using that dependencies, I get this kind of error: 使用这种依赖关系,我会遇到这种错误:

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)

In this case what could be the problem? 在这种情况下可能是什么问题?

Below the complete pom.xml file: 在完整的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>WebContent\WEB-INF\web.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>

As requested, following there is the web.xml file 根据要求,下面是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>

I think your issue is probably with the wrong version of servlet api. 我认为您的问题可能是servlet API版本错误。 Tomcat 7 supports 3.0 and below but it seems that you are using servlet version 3.1. Tomcat 7支持3.0及更低版本,但是您似乎正在使用Servlet 3.1版。

Your exception shows this method not found and this is only part of servlet 3.1 or above. 您的异常表明未找到此方法,并且该方法仅是Servlet 3.1或更高版本的一部分。

Since: 
 Servlet 3.1

Even though you have not included the web.xml in the question, the stacktrace leads to this resolution 即使您没有在问题中包含web.xml,stacktrace也会导致此解决方案

Please see your web.xml has this declaration. 请查看您的web.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">

Update: 更新:

You are using version 2.1 for tomcat7-maven-plugin which uses tomcat version 7.0.37. 您正在使用2.1版本的tomcat7-maven-plugin ,它使用的是Tomcat版本7.0.37。 And then in the dependencies you are trying to use tomcat jars with a different version 7.0.67. 然后在依赖项中,您尝试使用具有其他版本7.0.67的tomcat jar。 You must use the same version for both. 两者必须使用相同的版本。

You have two possibilities: 您有两种可能性:

  1. Either set the version of all jars to 7.0.37 将所有jar的版本都设置为7.0.37

  2. You can use the latest version 2.2 of tomcat7-maven-plugin and use the property for setting the tomcat version . 您可以使用最新版本的tomcat7-maven-plugin 2.2,并使用该属性设置tomcat版本 I would prefer this one. 我希望这个。 May be you can set this property with 2.1 also. 也许您也可以使用2.1设置此属性。 You can try it. 你可以试试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM