首页 > 解决方案 > Springboot 手动 JAR 无法正常运行

问题描述

我在 Springboot 中开发了一个 REST API 端点,当我访问时会显示一个问候语:

localhost:8081/hello

我有信心将其导出为“胖罐”(独立应用程序),使用

mvn clean install

现在,问题是当我访问相同的 '/hello' URL 端点时,我得到了这个:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Oct 23 09:15:15 PST 2020
There was an unexpected error (type=Not Found, status=404).

到底发生了什么?我确保我在 pom.xml 文件中指定了我的主类。

我正在使用 SpringToolSuite 4 IDE。

标签: javaspringspring-bootrestweb-services

解决方案


我认为Baeldung文章的指南可以帮助您,我将总结并添加我自己的:

  1. 确保具有以下依赖项:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
       </dependencies>
    
      <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.0.1.RELEASE</version>
        </plugin>
      </plugins> ```
    
    
  2. 只是为了确保指定您的主类,例如:

    <start-class>com.microservices.MyMainClass</start-class>
    
  3. 确保您 'mvn clean install' 在正确的目录上执行

  4. 如果您想将其打包为“WAR”文件,请使用

    <packaging> war </packaging>

在“项目”属性中。


推荐阅读