SpringBoot在本地开发通过,部署到服务器上失败,无法访问

3,627 阅读1分钟

今天刚刚完成了SpringBoot的一个微服务,于是将其打包成war包,然后放在了tomcat中的webapps目录下,可是访问时出现了404错误,如下:image
tomacat启动时日志显示
image 第一个显示了Apache Tomcat/8.0.53,说明tomcat服务没有问题,而是自己项目问题。在启动日志中,可以看出找不到该路由,说明该服务没有启动。

问题原因

该问题是因为自己打包时没有指明启动类,tomcat找不到项目的启动类。俗话就是,即tomcat解压放进去的war包后,找不到将war包运行起来的入口,相当于找不到项目的“主函数”,不知怎样将它运行起来。故服务没有运行。

解决方法

需要在项目中指明启动类,即声明项目的“主函数”,在项目的pom.xl中添加如下代码

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
              <!--此处为自己的启动类-->
              <mainClass>com.test.app.App</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<mainClass></mainClass>中写入自己的服务启动类,我的是com.test.app.App,如图: image
改完pom.xml后,使用mvn clean package 重新打包放到tomcat下的webapps目录下。 出现如下消息即为运行成功。
image

新手上车,请多指教,如有问题,请邮件联系:young5678@qq.com

参考

此知识点来自于蚂蚁课堂视频