You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成JSF配置Undertow同时支持HTTP和HTTPS启动报错如何解决

问题根因

你自定义的ServletWebServerFactory Bean直接实例化了空的UndertowServletWebServerFactory,完全覆盖了Spring Boot自动配置的Undertow工厂实例。自动配置的实例会自动加载application.properties中配置的SSL参数、Servlet上下文初始化参数(包含JSF运行所需的Factory注册配置),而手动创建的空实例缺少上述配置,导致FacesServlet初始化时无法找到FacesContextFactory实现,抛出对应异常。

修复方案

直接使用WebServerFactoryCustomizer对自动配置的Undertow工厂做扩展,不需要替换整个工厂Bean,既能保留所有自动配置的参数,又能额外添加HTTP监听端口。

步骤1:删除原有自定义Bean

删除WebConfig.java中你写的undertowWebServerFactory方法。

步骤2:添加Undertow定制器Bean

在WebConfig.java中添加如下代码:

import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Value;

// 类中其他代码保持不变
@Value("${server.address}")
private String serverAddress; // 注意修正原拼写错误serverAdress,避免配置绑定失败

@Value("${server.http.port}")
private int httpPort;

@Bean
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> undertowHttpListenerCustomizer() {
    return factory -> factory.addBuilderCustomizers(
            builder -> builder.addHttpListener(httpPort, serverAddress)
    );
}

步骤3:完善application.properties配置

确保配置中已指定HTTPS的默认端口:

# HTTPS端口,默认的server.port对应HTTPS服务端口
server.port=8443
# 自定义HTTP端口
server.http.port=8080
server.address=0.0.0.0
# 原有SSL配置保持不变
server.ssl.key-store-type=PKCS12
server.ssl.key-store=classpath:keystore
server.ssl.key-store-password=dummypwd
server.ssl.enabled-protocols=TLSv1.2
验证效果

启动应用后:

  • HTTPS服务使用server.port配置的端口(示例为8443)访问
  • HTTP服务使用server.http.port配置的端口(示例为8080)访问
    二者可同时正常响应请求。

内容的提问来源于stack exchange,提问作者s_cisse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 11:45:01