java.lang.NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()Ljava / lang / String;

时间:2017-04-12 21:51:02

标签: java spring cxf

我正在使用Java,Apache Cxf和Spring Frame工作构建Web服务 建立:Maven 部署:Tomcat 8.0.42 web.xml 中使用的servlet版本为2.5。当我将 WAR 部署到tomcat并启动服务器时,我收到以下错误。我已经尝试了堆栈中提供的许多解决方案,但似乎都没有工作。我已经检查了maven依赖项中的servlet-api.jar,它也不存在。还将web.xml中的servlet版本作为3.0,但它似乎也没有解决问题。需要良好的投入。 提前致谢

    Apr 12, 2017 5:24:58 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SAMPLE-WS]]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SAMPLE-WS]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
    ... 6 more
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
    at org.apache.tomcat.websocket.server.WsServerContainer.<init>(WsServerContainer.java:150)
    at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131)
    at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    ... 6 more

的web.xml

<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_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>SAMPLE-WS</display-name>

   <!--
        - Location of the XML file that defines the root application context.
        - Applied by ContextLoaderListener.
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/sample-cxf-beans.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <description>Apache CXF Endpoint</description>
        <display-name>cxf</display-name>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>


</web-app>

的pom.xml

<?xml version="1.0"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <modelVersion>4.0.0</modelVersion>

      <groupId>com.javaws.sample.ws</groupId>
      <artifactId>SAMPLE-WS</artifactId>
      <packaging>war</packaging>
      <name>SAMPLE-WS</name>


     <properties>
        <project.wsdl.path>src/main/webapp/WEB-INF/WSDLs/Sample_1.wsdl</project.wsdl.path>
        <target.wsdl.path>WEB-INF/WSDLs/Sample_1.wsdl</target.wsdl.path>

      </properties>
      <dependencies>
            <!-- Spring Web -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </dependency>
            <!--  Apache CXF -->
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-simple</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxws</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-transports-http</artifactId>
            </dependency>  
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-bindings-soap</artifactId>
            </dependency>
            <dependency>
              <groupId>org.apache.cxf</groupId>
              <artifactId>cxf-rt-transports-http-jetty</artifactId>
           </dependency>


        </dependencies>


    </project>

1 个答案:

答案 0 :(得分:1)

方法getVirtualServerName已添加到Servlet 3.1中的ServletContext中。找到java doc的方法getVirtualServerName

这个问题可能至少有两个原因:

  1. 您的servlet版本早于3.1。

  2. 其他jar的servlet版本低于3.1

  3. 要解决它,你可以尝试以下方式。

    予。检查你的pom.xml是否有以下代码。

      <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>javax.servlet-api</artifactId>
           <version>3.1.0</version>
        </dependency>
    

    如果你的pom.xml有上面的代码,它仍然会有这个问题。你可以做第二种方式。

    II。检查你的其他jar有没有引用javax.servlet-api jar。例如,org.apache.santuario引用了javax.servlet-api jar。 pom.xml:

    <dependency>  
        <groupId>org.apache.santuario</groupId>  
        <artifactId>xmlsec</artifactId>  
        <version>1.4.3</version>   
    </dependency> 
    

    但是当你查看maven依赖项时,它会引用版本比2.3早于3.1的javax.servlet-api jar。

    enter image description here

    所以你应该排除2.3版本。 pom.xml中:

    <!-- exclude servlet-api 2.3 jar-->  
    <dependency>  
        <groupId>org.apache.santuario</groupId>  
        <artifactId>xmlsec</artifactId>  
        <version>1.4.3</version>  
        <exclusions>  
            <exclusion>  
                <groupId>javax.servlet</groupId>  
                <artifactId>servlet-api</artifactId>  
            </exclusion>  
        </exclusions>  
    </dependency>  
    
    <!-- servlet-api 3.1 version has getVirtualServerName() -->  
    <dependency>  
        <groupId>javax.servlet</groupId>  
        <artifactId>javax.servlet-api</artifactId>  
        <version>3.1.0</version>  
    </dependency>