tomcat运行springboot项目war包
以最简单的spring boot demo项目来演示如何发布项目war包到tomcat,并成功运行(有很多小伙伴会出现404错误)
一、准备一个最简单的demo项目
在IDEA中新建一个项目,一直next,选择Dependencies中选择Web。
点击Finish完成
二、为了项目发布后方便排错,在DemoApplication中增加几行代码
package com.example.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
public class DemoApplication{ public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @GetMapping("/hello")
public String hello() {
return "This is a demo project.";
}
}
三、把项目打成war包
把项目打成war包需要修改两个地方:
1.DemoApplication继承SpringBootServletInitializer,重写configure方法
package com.example.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
public class DemoApplication extends SpringBootServletInitializer { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @GetMapping("/hello")
public String hello() {
return "This is a demo project.";
} @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class);
}
}
2.修改POM文件,把packaging修改为war,并在build中新增finalName为demo,指定打包之后的名称为demo.war,完整的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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<packaging>war</packaging>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
3.在IDEA界面的左下角,找到Terminal,并点击。输入mvn clean package -Dmaven.skip.tree=true回车
可以看到在target目录下,生成了demo.war
四、将target下的demo.war包复制到tomcat的webapps目录下,并启动tomcat
在浏览器访问localhost:8080,查看tomcat是否启动
在浏览器访问localhost:8080/demo/hello
表示项目已经部署成功。
五、排错,特别是404错误
排错步骤为:
1.首先需要确保demo项目,在idea中启动时,能正常访问。
2.如果localhost:8080无法看到tomcat的欢迎界面,需要排查端口是否正确,如果端口没有问题,则一定是tomcat的配置出现了问题。
3.如果能看到tomcat的欢迎界面,但是访问项目出现404,则首先需要排查tomcat的启动日志,查看是否报错。
4.如果启动日志没有报错,可则新建一个index.html,并把该index.html放入到tomcat/webapps/demo的根目录,通过localhost:8080/demo/index.html查看是否能正常访问。index.html的代码即为一个最简单的html默认页面。
5.如果不能访问,说明tomcat加载demo项目失败。如果能正常访问index.html,但访问demo/hello出现404,则需要排查是否是个web项目,也就是引入了spring-boot-starter-web依赖,项目的打包方式是否是war包,并且特别是启动类(这里是DemoApplication)是否继承了SpringBootServletInitializer,并重写了configure方法。
至于其他的业务报错,连接资源报错,不在此讨论范围。
tomcat运行springboot项目war包的更多相关文章
- Docker实现运行tomcat并部署项目war包,并实现挂载目录
之前写的有点乱,现在再来整理一下docker的简单部署运行 借鉴博客:https://blog.csdn.net/qq_32351227/article/details/78673591 一.dock ...
- springBoot 项目war包部署及改为war包后资源路径错误问题
参考资料: https://blog.csdn.net/rico_zhou/article/details/83415114 https://blog.csdn.net/pz641/article/d ...
- SpringBoot项目war包部署
服务部署 记录原因 将本地SpringBoot项目通过war包部署到虚拟机中,验证服务器部署. 使用war包是为了方便替换配置文件等. 工具 对象 版本 Spring Boot 2.4.0 VMwar ...
- springboot项目war包部署及出现的问题Failed to bind properties under 'mybatis.configuration.mapped-statements[0].
1.修改pom文件 修改打包方式 为war: 添加tomcat使用范围,provided的意思即在发布的时候有外部提供,内置的tomcat就不会打包进去 <groupId>com.scho ...
- shell脚本运行springboot项目jar包
#!/bin/bash APP_NAME=AutomationGuide-0.0.1-SNAPSHOT.jar #使用说明,用来提示输入参数 usage() { echo "please e ...
- Springboot项目打成jar包运行 和 打成war包 外部tomcat运行
Jar打包方式运行 类型为jar时 <packaging>jar</packaging> 1.使用命令mvn clean package 打包 2.使用java –jar 包 ...
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中
前言:本项目基于maven构建 spring-boot项目可以快速构建web应用,其内置的tomcat容器也十分方便我们的测试运行: spring-boot项目需要部署在外部容器中的时候,spring ...
- Springboot解决war包放到Tomcat服务器上404的特殊情况
Springboot解决war包放到Tomcat服务器上404的特殊情况 原文链接:https://www.cnblogs.com/blog5277/p/9330577.html 原文作者:博客园-- ...
- springboot项目jar包运行
springboot项目jar包运行 参考 Linux后台运行java的jar包 步骤 进入maven项目中,打包项目. mvn package -Dmaven.test.skip=true 运行ja ...
随机推荐
- Jenkins使用FTP进行一键部署及回滚2(Windows)(项目实践)
转载:http://www.cnblogs.com/EasonJim/p/6295372.html Jenkins使用FTP进行一键部署及回滚2(Windows)(项目实践) 前提: 这一篇是继上一 ...
- Problem of Uninstall Cloudera: Cannot Add Hdfs and Reported Cannot Find CDH's bigtop-detect-javahome
1. Problem We wrote a shell script to uninstall Cloudera Manager(CM) that run in a cluster with 3 li ...
- typedef char int8; 这样定义的好处?
用typedef定义int8代表char:然后用int8去定义其他变量.一旦系统中char不再是占8位的数据时,可重新typedef新的占8位的类型为int8,而所有的用int8定义的8为类型数不用再 ...
- 跟微软保持适当距离--Hessian + .net 实现RPC体系的企业应用
同在一个产业链园区的XX厂因为5台Window2003服务器收到了律师函并且被迫下了12万$的采购单,虽然100万对XXX厂来数不是大数目,但是总有种被打劫的感觉. 在企业ERP应用中服务层一般都是做 ...
- break,continue以及pass的使用
1.break是提前结束循环 for i in range(1,100): if i%2 == 0: print("wrong") break#直接结束循环,并且不打印下面的pri ...
- 在Github注册账户
https://github.com/JasonHaoz
- 如何将图片嵌入到Html中
将图片内嵌入到Html中,最好的方法就是用Base64 string.例如:<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUg ...
- ffmpeg学习(一)——在window7下编译ffmpeg
FFmpeg是一个开源免费跨平台的视频和音频流项目,它提供了录制.转换以及流化音视频的完整解决方案.本文作者将尝试使用该库实现一个可适应复杂网络环境的, 支持标准rtsp协议的流媒体服务器.由于Vis ...
- 对virtual虚方法的理解
对virtual虚方法的理解 https://www.cnblogs.com/u3ddjw/p/6676485.html
- 杭州.Net 相关大公司,希望对大家有帮助
本人目前大四,还在实习.北京工作辞职后,打算回杭州看看.发现杭州的大公司相对北京好少啊,招.Net相关的公司就更少了... (我认为刚毕业生还是去大公司比较靠谱,一方面也是实力的体现)大学生,而且之前 ...