我的应用程序在从 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
。
您的类(class)将如下所示:
@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>
您不需要它或将它的范围标记为已提供
此外,您可能需要像这样从 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
将无法工作。
关于java - 如何在没有 IntelliJ 的情况下在本地主机上运行 Spring 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59347800/