英文:
How can I change server in spring boot application?
问题
> 我的项目要求使用除了Tomcat之外的另一个服务器?我们如何从Spring Boot应用程序中更改嵌入式服务器?
英文:
> My project requirement is to use another server than tomcat? How can
> we change embedded server from spring boot application?
答案1
得分: 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>
然后你需要将新的服务器作为依赖包含,例如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
英文:
You have to exclude tomcat from starter dependency:
<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>
and now you need to include new server as a dependency i.e.:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
答案2
得分: 6
你需要更新 pom.xml
文件,在其中添加对 spring-boot-starter-jetty
依赖的声明。同时,你需要在 spring-boot-starter-tomcat
依赖声明中使用 exclude
来排除默认添加的 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>
对于 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")
}
英文:
You will need to update pom.xml
, add the dependency for spring-boot-starter-jetty
. Also, you will need to exclude
default added spring-boot-starter-tomcat
dependency.
<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>
In 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")
}
答案3
得分: 1
# Gradle方式
implementation 'org.springframework.boot:spring-boot-starter-jetty'
modules {
module("org.springframework.boot:spring-boot-starter-jetty") {
replacedBy("org.springframework.boot:spring-boot-starter-tomcat", "使用Jetty替代Tomcat")
}
}
# 另一种Gradle方式
compile('org.springframework.boot:spring-boot-starter-webflux') {
exclude group: 'org.springframework.boot',
module: 'spring-boot-starter-reactor-netty'
}
compile('org.springframework.boot:spring-boot-starter-jetty')
英文:
# Gradle way
implementation 'org.springframework.boot:spring-boot-starter-tomcat'
implementation 'org.springframework.boot:spring-boot-starter-jetty'
modules {
module("org.springframework.boot:spring-boot-starter-tomcat") {
replacedBy("org.springframework.boot:spring-boot-starter-jetty", "Use Jetty instead of Tomcat")
}
}
# Gradle Another Way
compile('org.springframework.boot:spring-boot-starter-webflux') {
exclude group: 'org.springframework.boot',
module: 'spring-boot-starter-reactor-netty'
}
compile('org.springframework.boot:spring-boot-starter-tomcat')
答案4
得分: 0
默认的 Spring Boot
中的 嵌入式Web服务器
是 Tomcat
,但你可以很容易地将其更改为其他服务器。
首先,你需要排除掉 Tomcat 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除 Tomcat 依赖 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
然后你可以添加你所需的嵌入式Web服务器:
<!-- Jetty 嵌入式Web服务器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
或者
<!-- Undertow 嵌入式Web服务器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
英文:
The default Embedded Web Servers
in Spring-Boot
is Tomcat
, but you can easily change it to others.
First of all, you need to exclude tomcat:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Then you can add your desired embedded web server:
<!-- Jetty Embedded Web Server -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
or
<!-- Undertow Embedded Web Server -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论