zoukankan      html  css  js  c++  java
  • 使用intellij idea打包并部署到外部的tomcat

    1.使用intellij idea创建项目demotest

      File -> New -> Project-> Spring Initializr,根据提示一步步操作

      会生成一个带有 main() 方法的类 DemotestApplication,用于启动应用程序

    2.新建class,HelloController

    package com.example.demotest;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    @RestController
    public class HelloController {
        @RequestMapping("/hello")
        public String hello() {
            return "Hello Spring Boot!";
        }
    }

    3.运行

    在DemotestApplication这个类中,然后右键点击运行

    浏览器中输入http://localhost:8080/hello

    4.打war包

      (1)Run-> Edit Configurations

      

      OK

      (2)修改pom.xml

        war包

    <packaging>war</packaging>

        添加一个spring-boot-maven-plugin打包插件,打包后的名字为packageTest

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
            <finalName>packageTest</finalName>
        </build>

      运行mvn就可以生成jar包

      

      输出下面提示,打包成功

      

      在项目的target目录可以看到生成的war包

       

      打开cmd,到war 包所在目录 运行命令

    java -jar  packageTest.war

      浏览器输入http://localhost:8080/hello

      输出hello信息

    4.发布到外部的tomcat

      把webtest.war放到tomcat下的webapp下,重启tomcat,

      

      解决当前这个在外部tomcat没办法运行起来并访问的问题,设置启动配置

      新建一个SpringBootStartApplication 继承自 SpringBootServletInitializer

        说明:

         A.在外部容器部署的话,不能依赖于Application的main函数

           B.在启动类中继承SpringBootServletInitializer并实现configure方法,以类似于web.xml文件配置的方式来启动Spring应用上下文

           C.新建的类与springboot的启动类是同级的

    package com.example.demotest;
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    public class SpringBootStartApplication extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(
                SpringApplicationBuilder application) {
            //DemotestApplication类为springboot 项目的启动类
            return application.sources(DemotestApplication.class);
        }
    }

       

    重新打包

      把webtest.war放到tomcat下的webapp下,重启tomcat

      

      发布成功

    补充:

      如果,设置启动配置后还是不行,需要让springboot内嵌的tomcat在运行时不起作用,修改pom.xml

      因为SpringBoot默认的容器为Tomcat,依赖包在spring-boot-starter-web下

      使用如下方式:

        <!--web支持-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

        或者

        <!--web支持-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        <!--声明spring boot内嵌tomcat的作用范围  在运行时不起作用-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
  • 相关阅读:
    删除表空间的时候遇到的问题:ORA-02429: 无法删除用于强制唯一/主键的索引
    删除
    Activity中使用getSystemService获得系统服务
    用多线程实现反应灵敏的界面(Java)
    用数组实现3个栈之固定分割(Java)
    Android小知识点20条
    Android数据库中数据文件的导出与查看
    MFC中使用CSplitterWnd分割窗口后设置视图大小的问题
    MFC中,通过preCreateWindow函数无法设置视图样式(包括窗口的大小)
    6:Node.js 路由
  • 原文地址:https://www.cnblogs.com/baby123/p/9354349.html
Copyright © 2011-2022 走看看