繁体   English   中英

如何在没有 IntelliJ 的情况下在本地主机上运行 Spring 应用程序?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM