springboot项目发布到tomcat容器

1.下面我简单写个例子,springboot项目发布到tomact容器中

2.新建一个maven工程,在pom.xml添加如下

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zhlk</groupId>
  <artifactId>spring-boot</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-boot Maven Webapp</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
 <dependencies>
 		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
  <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
   </dependencies>
   <build>
     <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
            <warName>springboot</warName>
            </configuration>
  </plugin> 
 </plugins>
   </build>
</project>

其中,如下是生成war

 <packaging>war</packaging>

其中,如下依赖是为了脱离项目自带的tomcat容器

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
如下依赖是为了更改项目应用工程名   

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
            <warName>springboot</warName>
            </configuration>
  </plugin> 

3.新建一个HelloApplication类,如下

package com.ly.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Configuration
@SpringBootApplication
public class HelloApplication extends SpringBootServletInitializer {

	@RequestMapping("hello")
	@ResponseBody
	public String hello(){
		return "hello";
	}
	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(HelloApplication.class);
    }
	public static void main(String[] args) {
		SpringApplication app=new SpringApplication(HelloApplication.class);
		app.run(args);
	}
}

4.保存pom.xml,使项目加载完所需要的包,项目右键maven更新一下,之后,项目右键Run As -Maven build一下 ,运行之后就可以自动生成war


5.找到springboot.war,放到tomact下面的webapps目录下,解压也行,不解压也行,tomact启动自动会解压的

6.启动tomcat,访问:http://127.0.0.1:8080/springboot/hello








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值