springboot项目发布到tomcat容器

来源:互联网 发布:朴廷桓柯洁网络十番棋 编辑:程序博客网 时间:2024/06/06 21:32

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@SpringBootApplicationpublic class HelloApplication extends SpringBootServletInitializer {@RequestMapping("hello")@ResponseBodypublic 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