[英]How to run Spring app on localhost without IntelliJ?
我的應用程序在從 IntelliJ 運行時工作,但我在嘗試在沒有 IntelliJ 幫助的情況下運行它時遇到問題。
主要類:
@SpringBootApplication
public class LoadingsApplication {
public static void main(String[] args) {
SpringApplication.run(LoadingsApplication.class, args);
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pl.margol</groupId>
<artifactId>loadings</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>loadings</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
應用程序屬性:
server.port=8087
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/loadings-db;
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update
.war 文件是在 Tomcat Web 應用程序管理器中創建和部署的。 當我調用localhost:8080/loadings-0.0.1-SNAPSHOT/start
我得到錯誤:
HTTP Status 404 – Not Found
Type Status Report
Message /loadings-0.0.1-SNAPSHOT/start
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.20
我試圖更改 application.properties 文件中的端口,但沒有幫助。 你能幫忙解決這個問題嗎?
我認為您的應用程序根本沒有部署。
由於您將應用程序部署為外部容器中的 war 文件,因此您需要擴展SpringBootServletInitializer
。 你的類看起來像這樣:
@SpringBootApplication
public class LoadingApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(HelloServerApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
{
return builder.sources(HelloServerApplication.class);
}
}
為什么要使用此依賴項:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
您不需要它或將其范圍標記為provided
此外,您可能需要像這樣從spring-boot-starter-web
排除 tomcat:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
而且由於您使用的是外部容器server.port=8087
將不起作用。
嘗試這個。
@SpringBootApplication
public class LoadingsApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(LoadingsApplication.class, args);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.