我创建了一个非常简单的Spring(无 Boot )应用程序,我可以将其打包到.war中,并手动部署到Tomcat中,它可以正常工作。以下是我的项目:https://github.com/gaetan-l/demo-spring-api
现在我想在this github project之后使用嵌入式Tomcat,所以我添加了以下类:
package gaetanl.demo.springapi;
import java.io.File;
import java.util.Optional;
import org.apache.catalina.startup.Tomcat;
public class Application {
public static final Optional<String> port = Optional.ofNullable(System.getenv("PORT"));
public static void main(String[] args) throws Exception {
String webappPath = "./build/libs";
String warName = "demo-spring-api-1.0-SNAPSHOT";
File war = new File(webappPath + "/" + warName + ".war");
if (!war.exists()) {
System.out.println("The .war has not been created yet (or is in another folder than ./build/libs)");
System.out.println("Try running the gradle task \"assemble\"");
return;
}
Tomcat tomcat = new Tomcat();
tomcat.setPort(Integer.valueOf(port.orElse("8080") ));
// Tomcat base directory
tomcat.setBaseDir("./tomcat.8080");
// Application root for this Host, can be an absolute pathname, a
// relative pathname, or a URL.
tomcat.getHost().setAppBase("./work/Tomcat/localhost");
// Adding a web application to a host's appBase (usually Tomcat's
// webapps directory).
tomcat.addWebapp("/" + warName, war.getCanonicalPath());
tomcat.start();
tomcat.getServer().await();
}
}
.war在嵌入式Tomcat的webapp文件夹中正确解压缩,然后当我运行Application类时,服务器似乎正确启动,下面是控制台输出:
16:09:07: Executing ':Application.main()'...
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :Application.main()
sept. 28, 2023 4:09:08 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
sept. 28, 2023 4:09:08 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/10.1.13]
sept. 28, 2023 4:09:08 PM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
INFO: No global web.xml found
sept. 28, 2023 4:09:11 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
sept. 28, 2023 4:09:11 PM org.apache.catalina.core.ApplicationContext log
INFO: 1 Spring WebApplicationInitializers detected on classpath
sept. 28, 2023 4:09:11 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring DispatcherServlet 'dispatcher'
sept. 28, 2023 4:09:11 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Initializing Servlet 'dispatcher'
sept. 28, 2023 4:09:13 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Completed initialization in 1553 ms
但是我无法访问服务器,当我尝试执行curl.exe -v http://localhost:8080/时,我得到以下答案:
* Trying 127.0.0.1:8080...
* Trying [::1]:8080...
* connect to 127.0.0.1 port 8080 failed: Connection refused
* connect to ::1 port 8080 failed: Connection refused
* Failed to connect to localhost port 8080 after 2252 ms: Couldn't connect to server
* Closing connection 0
当我try netstat -an | find "8080"
它也找不到任何东西。我做错什么了吗?
1条答案
按热度按时间yuvru6vn1#
我在这里找到了答案:Simple Embedded Tomcat 10 Example
我需要在
tomcat.setPort(...)
之后添加tomcat.getConnector();