跳到主要内容

6. Tomcat

要使用 Tomcat 作为服务器,请添加以下依赖项:

implementation("io.micronaut.servlet:micronaut-http-server-tomcat:3.3.5")
注意

GraalVM 本地镜像支持 Tomcat

注意

如果你计划生成一个 WAR 文件,那么依赖应该是 developmentOnly

要自定义 Tomcat 服务器,可以使用以下配置属性:

表 1. TomcatConfiguration 配置属性

属性类型描述
micronaut.server.tomcat.serviceorg.apache.catalina.Service
micronaut.server.tomcat.allow-traceboolean
micronaut.server.tomcat.async-timeoutlong
micronaut.server.tomcat.discard-facadesboolean
micronaut.server.tomcat.enable-lookupsboolean
micronaut.server.tomcat.max-cookie-countint
micronaut.server.tomcat.max-parameter-countint
micronaut.server.tomcat.max-post-sizeint
micronaut.server.tomcat.max-save-post-sizeint
micronaut.server.tomcat.parse-body-methodsjava.lang.String
micronaut.server.tomcat.portint
micronaut.server.tomcat.port-offsetint
micronaut.server.tomcat.proxy-namejava.lang.String
micronaut.server.tomcat.proxy-portint
micronaut.server.tomcat.redirect-portint
micronaut.server.tomcat.schemejava.lang.String
micronaut.server.tomcat.secureboolean
micronaut.server.tomcat.uriencodingjava.lang.String
micronaut.server.tomcat.use-body-encoding-for-uriboolean
micronaut.server.tomcat.xpowered-byboolean
micronaut.server.tomcat.use-ipvhostsboolean
micronaut.server.tomcat.encoded-solidus-handlingjava.lang.String
micronaut.server.tomcat.domainjava.lang.String
micronaut.server.tomcat.throw-on-failureboolean
micronaut.server.tomcat.protocoljava.lang.String使用的协议。默认为 Defaults to org.apache.coyote.http11.Http11NioProtocol
micronaut.server.tomcat.attributesjava.util.Map连接器属性

或者你也可能注册一个 BeanCreatedEventListener

Tomcat Server 定制

import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import org.apache.catalina.startup.Tomcat;
import jakarta.inject.Singleton;

@Singleton
public class TomcatServerCustomizer implements BeanCreatedEventListener<Tomcat> {
@Override
public Tomcat onCreated(BeanCreatedEvent<Tomcat> event) {
Tomcat tomcat = event.getBean();
// perform customizations...
return tomcat;
}
}

英文链接