运行带有tomcat嵌入式9的项目时出现问题。 我的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();
}
}
然后注释掉servlet和servlet映射的web.xml,因为我只是尝试正确加载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>
运行main时我得到
"C:\Program Files\Java\jdk1.8.0_131\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:50567,suspend=y,server=n -Dvisualvm.id=64887079117098 -javaagent:C:\Users\sb30123\.IntelliJIdea2018.1\system\groovyHotSwap\gragent.jar -javaagent:C:\Users\sb30123\.IntelliJIdea2018.1\system\captureAgent\debugger-agent.jar=file:/C:/Users/sb30123/AppData/Local/Temp/capture.props -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;C:\repositories\embedded_tomcat_example\out\production\classes;C:\repositories\embedded_tomcat_example\out\production\resources;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-core\9.0.10\fbf9dbc97babe8425332e6baf3925e8f726f3d7b\tomcat-embed-core-9.0.10.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-jasper\9.0.10\761ea6f3b09c163f3710f5e811693c0e7a66b5e8\tomcat-embed-jasper-9.0.10.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-logging-juli\9.0.0.M6\2b083aa89d92ce10356158da1e643f8a8e890b49\tomcat-embed-logging-juli-9.0.0.M6.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.containers\jersey-container-servlet\2.27\aa97ee9f6528706d1d7214f39a404297903a7598\jersey-container-servlet-2.27.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.core\jersey-server\2.27\92c158af89e6749b20134c3182935046a49a1662\jersey-server-2.27.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.inject\jersey-hk2\2.27\9ea08f93ee7045a36926954536ad385ac03b753f\jersey-hk2-2.27.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.apache.tomcat\tomcat-annotations-api\9.0.10\18836b46b76d385615a738d5136fdbdaf9b0f32c\tomcat-annotations-api-9.0.10.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-el\9.0.10\7811474c12b6b3156a4b2c9d85d957c9542a7fd5\tomcat-embed-el-9.0.10.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.eclipse.jdt\ecj\3.13.102\d8061c14ff6388664299403791ae6bec487c12d7\ecj-3.13.102.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.containers\jersey-container-servlet-core\2.27\81e24b17ff7cee96a1c830fe63aa1215176c009c\jersey-container-servlet-core-2.27.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.core\jersey-common\2.27\f20d4304a476a92001930d75747adcc232dbe779\jersey-common-2.27.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\javax.ws.rs\javax.ws.rs-api\2.1\426a0862406536e690c7caa8bb6ed32191986fac\javax.ws.rs-api-2.1.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.core\jersey-client\2.27\66dd6a62a981c0f186d96048fe11e9ddeca3ae28\jersey-client-2.27.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.media\jersey-media-jaxb\2.27\106f2237ee03493185d9d915c9a1c011df716a2a\jersey-media-jaxb-2.27.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\javax.annotation\javax.annotation-api\1.2\479c1e06db31c432330183f5cae684163f186146\javax.annotation-api-1.2.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2.external\javax.inject\2.5.0-b42\98e0b7dcef77dc04809f0603868140a1c60bea71\javax.inject-2.5.0-b42.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\javax.validation\validation-api\1.1.0.Final\8613ae82954779d518631e05daa73a6a954817d5\validation-api-1.1.0.Final.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-locator\2.5.0-b42\9d6edc0cb226401a8316e67d81bfc37cb626ef91\hk2-locator-2.5.0-b42.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\osgi-resource-locator\1.0.1\4ed2b2d4738aed5786cfa64cba5a332779c4c708\osgi-resource-locator-1.0.1.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2.external\aopalliance-repackaged\2.5.0-b42\e74beab6ca12e9e745eb47ca61729d9452b96f0c\aopalliance-repackaged-2.5.0-b42.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-api\2.5.0-b42\7fd4e3d0f2cb37c80ad0dedee3f5ee69503eaf52\hk2-api-2.5.0-b42.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-utils\2.5.0-b42\102a344e1728825e865a8986d7605602aba3c3b6\hk2-utils-2.5.0-b42.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\org.javassist\javassist\3.22.0-CR2\44eaf0990dea92f4bca4b9931b2239c0e8756ee7\javassist-3.22.0-CR2.jar;C:\Users\sb30123\.gradle\caches\modules-2\files-2.1\javax.inject\javax.inject\1\6975da39a7040257bd51d21a231b76c915872d38\javax.inject-1.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.4\lib\idea_rt.jar" com.example.Main
Connected to the target VM, address: '127.0.0.1:50567', transport: 'socket'
Jul 05, 2018 11:57:24 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Jul 05, 2018 11:57:24 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/9.0.10
Jul 05, 2018 11:57:24 AM org.apache.tomcat.util.digester.Digester endElement
WARNING: No rules found matching 'web-app'.
Jul 05, 2018 11:57:25 AM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
INFO: No global web.xml found
*******根据@Rohi的评论进行编辑*******************
项目的构建者是
embedded_tomcat_example
|-src
| |-com
| | |-example
| | | |-Main.java
|-WebContent
| |-WEB-INF
| | |-web.xml
有人可以看到问题吗?
答案 0 :(得分:0)
更改为tomcat嵌入式8.0.52解决了该问题