Java 未能在tomcat embedded 9中加载web.xml

Java 未能在tomcat embedded 9中加载web.xml,java,tomcat,web.xml,Java,Tomcat,Web.xml,我在使用tomcat embedded 9运行项目时遇到了问题。 My web.xml来自tomcat web.xml示例 这是我的成绩: plugins { id 'java' id 'idea' } group 'com.example' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { // https://mvnreposito

我在使用tomcat embedded 9运行项目时遇到了问题。 My web.xml来自tomcat web.xml示例

这是我的成绩:

plugins {
  id 'java'
  id 'idea'
}

group 'com.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
  mavenCentral()
}

dependencies {
// https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core
  compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '9.0.10'

  // https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper
  compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '8.0.52'
  // https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-logging-juli
  compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-logging-juli', version: '8.0.52'

  // https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet
  compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.27'
  // https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server
  compile group: 'org.glassfish.jersey.core', name: 'jersey-server', version: '2.27'

  // https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2
  compile group: 'org.glassfish.jersey.inject', name: 'jersey-hk2', version: '2.27'

  testCompile group: 'junit', name: 'junit', version: '4.12'
}
主要

import javax.servlet.ServletException;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.Context;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import java.io.File;
import java.net.MalformedURLException;

public class Main {

  public static void main(String[] args) throws Exception, LifecycleException {
    new Main().start();
  }

  public void start() throws ServletException, LifecycleException,
                             MalformedURLException {

    // Define a folder to hold web application contents.
    String webappDirLocation = "WebContent/";
    Tomcat tomcat = new Tomcat();

    // Define port number for the web application
    String webPort = System.getenv("PORT");
    if (webPort == null || webPort.isEmpty()) {
      webPort = "8080";
    }
    // Bind the port to Tomcat server
    tomcat.setPort(Integer.valueOf(webPort));

    // Define a web application context.
    Context context = tomcat.addWebapp("/tomcatembedded", new File(
        webappDirLocation).getAbsolutePath());

    // Define and bind web.xml file location.
    File configFile = new File(webappDirLocation + "WEB-INF/web.xml");
    context.setConfigFile(configFile.toURI().toURL());

    tomcat.start();
    tomcat.getServer().await();
  }

}
还有web.xml,其中显示了servlet和servlet映射,因为我只是尝试正确加载web.xml

<?xml version="1.0" encoding="utf-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

  <!--<servlet>-->
    <!--<servlet-name>REST Servlet</servlet-name>-->
    <!--<servlet-class>-->
      <!--org.glassfish.jersey.servlet.ServletContainer-->
    <!--</servlet-class>-->
    <!--<init-param>-->
      <!--<param-name>jersey.config.server.provider.packages</param-name>-->
      <!--<param-value>com.example.resources</param-value>-->
    <!--</init-param>-->

    <!--<load-on-startup>1</load-on-startup>-->
  <!--</servlet>-->
  <!--<servlet-mapping>-->
    <!--<servlet-name>REST Servlet</servlet-name>-->
    <!--<url-pattern>/rest/*</url-pattern>-->
  <!--</servlet-mapping>-->

</web-app>
*******根据@Rohi的评论进行编辑*******************

项目的结构是

embedded_tomcat_example
|-src
|  |-com
|  |  |-example
|  |  |  |-Main.java
|-WebContent
|  |-WEB-INF
|  |  |-web.xml

有人能看到问题吗?

改成tomcat embedded 8.0.52解决了问题

你能显示你项目的目录结构吗?是的,我已经添加了你不应该使用
war
插件吗?war是为独立的tomcat设计的,不是吗?不管怎样,我已经试过了,但是没有change@rachel“找不到全局web.xml”消息只是一条信息消息,不是错误。如果你打开了怎么办?看(我不明白你到底失败了什么。)
embedded_tomcat_example
|-src
|  |-com
|  |  |-example
|  |  |  |-Main.java
|-WebContent
|  |-WEB-INF
|  |  |-web.xml