Spring Boot 使用 jetty 作为默认的Web容器

更换 SpringBoot 中默认的web 容器为 jetty 仅需两步

第一步:去除springboot 中默认的Tomcat 依赖。

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>

第二步:添加 jetty 依赖。

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jettyartifactId>
        dependency>

完成 POM 文件:


<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.0modelVersion>

    <groupId>com.china.testgroupId>
    <artifactId>springboot-webartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>springboot-webname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.8.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jettyartifactId>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>


project>

项目启动结果展示:

--- : jetty-9.4.7.v20170914
--- : DefaultSessionIdManager workerName=node0
--- : No SessionScavenger set, using defaults
--- : Scavenging every 660000ms
--- : Initializing Spring embedded WebApplicationContext
--- : Root WebApplicationContext: initialization completed in 4275 ms
--- : Mapping servlet: 'dispatcherServlet' to [/]
--- : Mapping filter: 'characterEncodingFilter' to: [/*]
--- : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
--- : Mapping filter: 'httpPutFormContentFilter' to: [/*]
--- : Mapping filter: 'requestContextFilter' to: [/*]
--- : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@7555b920{/,[file:///C:/Users/jetty/AppData/Local/Temp/jetty-docbase.966869451084382807.8080/],AVAILABLE}
--- : Started @6207ms
--- 
--- : Started ServerConnector@4152d38d{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
--- : Jetty started on port(s) 8080 (http/1.1)
--- : Started SpringbootWebApplication in 7.023 seconds (JVM running for 7.909)

你可能感兴趣的:(Spring,boot)