Springboot项目发布war到tomcat
springboot项目有些日子没有开发了,新做一个min spring boot项目,复习下项目开发及发布流程。
1.新建项目:
2.新建一个业务controller
controller名称及方法,名称随意,项目结构如下:
testcontroller代码文件的内容如下:
package com.*****.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class testController { @RequestMapping("/abc") public String abc() { return "ok."; } }
3.修改启动配置
然后在项目的启动文件APP.java中修改启动配置如下:
package com.*****; 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; /** * Hello world! * */ @SpringBootApplication public class App extends SpringBootServletInitializer { public static void main( String[] args ) { //System.out.println( "Hello World!" ); SpringApplication.run(App.class, args); } @Override //这个表示使用外部的tomcat容器 protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // 注意这里要指向原先用main方法执行的启动类 return builder.sources(App.class); } }
其中继承SpringBootServletInitializer及重载configure是为了打包war时使用。
4.修改Pom文件:
<?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> <groupId>com.*****</groupId> <artifactId>minispring</artifactId> <version>1.0-SNAPSHOT</version> <name>minispring</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> </parent> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!--打包不参与,也就是打包去掉tomcat--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <jvmArguments>Dfile.encoding=UTF-8</jvmArguments> <!--配置入口类的标签名--> <mainClass>com.*****.App</mainClass> </configuration> </plugin> </plugins> </build> </project>
其中,一般情况下,如果打包成jar包,则只需要引用spring-boot-maven-plugin即可,由于要打包为war包,则需要另外增加配置:tomcat-embed-jasper、spring-boot-starter-tomcat。
5.在maven中进行打包
打包完成后,将war包拷贝到tomcat的webapps目录下面(重命名为mi.war),运行tomcat即可自动解压war(tomcat默认自动解压war包,如果没有也可以手动配置)。
6.测试:
http://localhost:8080/mi/test/abc
参考地址:https://blog.csdn.net/led1114/article/details/122650051