如何在 Spring Boot 应用程序中更改服务器?

Vik*_*wat 5 java spring tomcat spring-boot

我的项目需求是使用tomcat以外的其他服务器?我们如何从 Spring Boot 应用程序更改嵌入式服务器?

Pla*_*ant 7

您必须从启动器依赖项中排除 tomcat:

<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>
Run Code Online (Sandbox Code Playgroud)

现在您需要包含新服务器作为依赖项,即:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)


Thi*_*mal 6

您将需要更新pom.xml、添加依赖项spring-boot-starter-jetty。此外,您将需要exclude默认添加的spring-boot-starter-tomcat依赖项。

<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在 gradle 中,

configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
}
 
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-jetty")
}
Run Code Online (Sandbox Code Playgroud)