Springboot 手册中的 JAR 文件无法正常运行。

huangapple go评论60阅读模式
英文:

Springboot Manual JAR not running properly

问题

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

localhost:8081/hello

我自信地将其导出为一个"fat jar"(独立应用程序),使用以下命令:

mvn clean install

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

Whitelabel错误页面
此应用程序没有针对/error的显式映射,所以您正在将其视为回退选项。

星期五,2020年10月23日09:15:15 PST
发生了意外错误(类型=未找到,状态=404)。

到底是怎么回事?
我确保在pom.xml文件中指定了我的主类。

我正在使用SpringToolSuite 4 IDE。

英文:

I developed a REST API endpoint in Springboot which displays a greeting when I access:

localhost:8081/hello

Feeling confident I export it as a 'fat jar' (standalone app) using

mvn clean install

Now, the problem is when I access the same '/hello' URL endpoint, I get this:

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).

What the hell happened?
I made sure I specifed my main class in the pom.xml file.

I am using SpringToolSuite 4 IDE.

答案1

得分: 1

我认为Baeldung文章中的指南可以帮助你,我会进行总结并添加我的观点:

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

    <dependencies>
        <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>
    

在“project”属性内部添加。

英文:

I think the guide by Baeldung article can help you, I'll summarize and add my own:

  1. Make sure to have these dependencies:

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
            &lt;version&gt;2.0.1.RELEASE&lt;/version&gt;
        &lt;/dependency&gt;
       &lt;/dependencies&gt;
    
      &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
            &lt;version&gt;2.0.1.RELEASE&lt;/version&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt; ```
    
    
  2. Just to be sure specify your main class like:

    &lt;start-class&gt;com.microservices.MyMainClass&lt;/start-class&gt;
    
  3. Make sure you are executing
    &#39;mvn clean install&#39;
    on the correct directory

  4. If you want to package it as 'WAR' file instead, use

    &lt;packaging&gt; war &lt;/packaging&gt;

inside the "project" properties.

huangapple
  • 本文由 发表于 2020年10月23日 09:17:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64492582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定