Springboot在tomcat下启动

tomcat下启动配置方法

有时候小项目并不像每次在服务器都用命令启动,偷个懒难道就不能用在tomcat下跑起来么,当然是可以的,其实只需启动类继承SpringBootServletInitializer

启动类继承SpringBootServletInitializer

@RestController
@SpringBootApplication
//mapper 接口类扫描包配置
@MapperScan("com.zjcjava.modulesapi.*.dao")
public class Application  extends SpringBootServletInitializer {//启动类继承SpringBootServletInitializer




    /*********
     * index入口
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    Object index ( ) {

        return "<h1>web-api project @ zjcjava 2018</h1>";
    }


    //启动main方法
    public static void main(String[] args)  {//throws Exception
        SpringApplication.run(Application.class, args);
    }



}

pom引入tomcat下运行需要的jar

<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.zjcjava</groupId>
    <artifactId>web-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 部署到tomcat中需要使用 -->
    <packaging>war</packaging>


    <name>xm-web-api</name>
    <url>http://maven.apache.org</url>

    <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <mybatis-spring-boot>1.2.0</mybatis-spring-boot>
        <mysql-connector>5.1.39</mysql-connector>
    </properties>

    <dependencies>

        <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Spring Boot Mybatis 依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>

        <!-- MySQL 连接驱动依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector}</version>
        </dependency>


        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>


        <!-- 部署到tomcat中需要使用 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>1.5.1.RELEASE</version>
        </dependency>



    </dependencies>



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <skipTests>true</skipTests>
                    <!-- 打成war包时名子 -->
                    <warName>web-api</warName>
                    <!-- mave时启用的main路径(因为其他包里面测试时也加了main函数) -->
                    <mainClass>${start-class}</mainClass>
                    <skip>true</skip><!-- 跳过测试 -->
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

说下注意点

  1. 打包的类型需要改成war
  2. 加入spring-boot-starter-tomcat需要的jar
  3. build编译时可以修改编译打包的项目名称,如果修改可以注释掉,但是我们在文本中访问的时候就必须以打包的文件名为根目录访问

maven打包

pom.xml右键Run as ->maven install
把编译打包后的文件(target文件夹下)
web-api.war
丢到tomcat下面,启动tomcat

我这里的项目访问路径如下,如果你不重名则直接是打包文件的文件名作为根目录
localhost:8080/web-api

这里写图片描述

发布了158 篇原创文章 · 获赞 72 · 访问量 33万+
展开阅读全文

SpringBoot 启动时 tomcat启动后会停止服务是如何解决

09-13

在eclipse 上对项目 运行 spring boot App 没有异常但tomcat 启动后会停止,这是什么原因? dome参照《Spring boot实战》第二章 版本信息 jdk1.8 本地Tomcat 9.0.11 Log ``` :: Spring Boot :: (v2.0.4.RELEASE) 九月 13, 2018 11:17:54 上午 org.apache.catalina.core.StandardService startInternal 信息: Starting service [Tomcat] 九月 13, 2018 11:17:54 上午 org.apache.catalina.core.StandardEngine startInternal 信息: Starting Servlet Engine: Apache Tomcat/9.0.11 九月 13, 2018 11:17:54 上午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 信息: Loaded APR based Apache Tomcat Native library [1.2.14] using APR version [1.6.2]. 九月 13, 2018 11:17:54 上午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 九月 13, 2018 11:17:54 上午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 信息: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true] 九月 13, 2018 11:17:55 上午 org.apache.catalina.core.AprLifecycleListener initializeSSL 信息: OpenSSL successfully initialized [OpenSSL 1.0.2l 25 May 2017] 九月 13, 2018 11:17:55 上午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring embedded WebApplicationContext 九月 13, 2018 11:17:55 上午 org.apache.catalina.core.StandardService stopInternal 信息: Stopping service [Tomcat] ``` pox.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> <groupId>com.example</groupId> <artifactId>readinglist</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>readinglist</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <tomcat.version>9.0.11</tomcat.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <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> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.3.0-alpha4</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-juli</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.7.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览