如何在Swagger-UI中预选中指定服务器(Spring Boot+Springdoc)
实现Swagger-UI根据环境自动预选中对应服务器
完全可行,核心思路是利用OpenAPI规范中支持设置默认服务器的特性,在构建Server列表时,根据当前运行环境给对应服务器标记为默认,Swagger-UI会自动选中默认服务器,同时保持原有列表顺序不变。
具体实现步骤
1. 方式一:通过环境标识匹配(推荐)
利用Spring的Environment获取当前激活的环境配置,给对应描述的Server设置默认值:
import org.springframework.core.env.Environment; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.servers.Server; import java.util.Arrays; import java.util.List; @Component public class CustomOpenApiCustomiser implements OpenApiCustomiser { private final Environment environment; private final String localUrl; private final String dsoUrl; private final String iftUrl; private final String prodUrl; // 从配置文件读取各环境URL,注入环境变量 public CustomOpenApiCustomiser(Environment environment, @Value("${app.server.local}") String localUrl, @Value("${app.server.dso}") String dsoUrl, @Value("${app.server.ift}") String iftUrl, @Value("${app.server.prod}") String prodUrl) { this.environment = environment; this.localUrl = localUrl; this.dsoUrl = dsoUrl; this.iftUrl = iftUrl; this.prodUrl = prodUrl; } @Override public void customise(OpenAPI openApi) { List<Server> servers = List.of( new Server().url(localUrl).description("localhost"), new Server().url(dsoUrl).description("DSO"), new Server().url(iftUrl).description("IFT"), new Server().url(prodUrl).description("PROD") ); // 获取当前激活的环境,默认 fallback 到localhost String activeProfile = Arrays.stream(environment.getActiveProfiles()) .findFirst() .orElse("localhost"); // 给对应环境的服务器标记为默认 servers.forEach(server -> { switch (activeProfile) { case "dso": server.setDefault(server.getDescription().equals("DSO")); break; case "ift": server.setDefault(server.getDescription().equals("IFT")); break; case "prod": server.setDefault(server.getDescription().equals("PROD")); break; default: server.setDefault(server.getDescription().equals("localhost")); } }); openApi.servers(servers); } }
2. 方式二:通过当前运行URL匹配
如果不需要依赖环境标识,可通过应用运行的实际URL自动匹配对应的Server:
import org.springframework.boot.web.server.ServerProperties; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.servers.Server; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.List; @Component public class CustomOpenApiCustomiser implements OpenApiCustomiser { private final ServerProperties serverProperties; private final String localUrl; private final String dsoUrl; private final String iftUrl; private final String prodUrl; public CustomOpenApiCustomiser(ServerProperties serverProperties, @Value("${app.server.local}") String localUrl, @Value("${app.server.dso}") String dsoUrl, @Value("${app.server.ift}") String iftUrl, @Value("${app.server.prod}") String prodUrl) { this.serverProperties = serverProperties; this.localUrl = localUrl; this.dsoUrl = dsoUrl; this.iftUrl = iftUrl; this.prodUrl = prodUrl; } @Override public void customise(OpenAPI openApi) { String currentUrl; try { // 拼接当前应用的基础URL(主机名+端口+上下文路径) currentUrl = "http://" + InetAddress.getLocalHost().getHostName() + ":" + serverProperties.getPort() + serverProperties.getServlet().getContextPath(); } catch (UnknownHostException e) { // 异常时默认使用localhost地址 currentUrl = localUrl; } List<Server> servers = List.of( new Server().url(localUrl).description("localhost"), new Server().url(dsoUrl).description("DSO"), new Server().url(iftUrl).description("IFT"), new Server().url(prodUrl).description("PROD") ); // 匹配当前URL对应的Server并设置为默认 servers.forEach(server -> server.setDefault(server.getUrl().equals(currentUrl))); openApi.servers(servers); } }
注意事项
- 确保
localUrl、dsoUrl等参数通过对应环境的配置文件(如application-dso.yml)配置正确的地址; - 方式一中需在各环境配置文件中设置
spring.profiles.active为对应标识(如application-dso.yml中设置spring.profiles.active=dso); - OpenAPI规范中仅会有一个Server生效为默认,遍历过程中只会给匹配到的那个Server设置
default=true; - 服务器列表的顺序完全保留原配置,仅修改对应项的默认标记。
内容的提问来源于stack exchange,提问作者Mikhail Geyer
相关产品推荐
相关产品推荐

