跳到主要内容

5. Jetty Server

要将 Jetty 用作服务器,请添加以下依赖项:

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

GraalVM 本地镜像支持 Jetty

注意

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

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

表 1. 为 JettyConfiguration 配置属性

属性类型描述
micronaut.server.jetty.idle-timeoutlong
micronaut.server.jetty.persistent-connections-enabledboolean
micronaut.server.jetty.send-server-versionboolean
micronaut.server.jetty.send-xpowered-byboolean
micronaut.server.jetty.send-date-headerboolean
micronaut.server.jetty.delay-dispatch-until-contentboolean
micronaut.server.jetty.customizersjava.util.List
micronaut.server.jetty.output-buffer-sizeint
micronaut.server.jetty.output-aggregation-sizeint
micronaut.server.jetty.request-header-sizeint
micronaut.server.jetty.response-header-sizeint
micronaut.server.jetty.header-cache-sizeint
micronaut.server.jetty.secure-portint
micronaut.server.jetty.secure-schemejava.lang.String
micronaut.server.jetty.form-encoded-methodsjava.lang.String
micronaut.server.jetty.max-error-dispatchesint
micronaut.server.jetty.min-request-data-ratelong
micronaut.server.jetty.min-response-data-ratelong
micronaut.server.jetty.request-cookie-complianceorg.eclipse.jetty.http.CookieCompliance
micronaut.server.jetty.response-cookie-complianceorg.eclipse.jetty.http.CookieCompliance
micronaut.server.jetty.multi-part-form-data-complianceorg.eclipse.jetty.server.MultiPartFormDataCompliance
micronaut.server.jetty.notify-remote-async-errorsboolean
micronaut.server.jetty.relative-redirect-allowedboolean
micronaut.server.jetty.local-addressjava.net.SocketAddress
micronaut.server.jetty.server-authorityorg.eclipse.jetty.util.HostPort
micronaut.server.jetty.init-parametersjava.util.Map设置 servlet 初始化参数。

或者,你可以注册一个 BeanCreatedEventListener

Jetty Server 自定义

import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import org.eclipse.jetty.server.Server;
import jakarta.inject.Singleton;

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

英文链接