springdoc-openapi依赖包
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
<version>2.1.0</version>
</dependency>
关闭方式
springdoc:
swagger-ui:
enabled: false
关键部分代码分析
package org.springdoc.core.properties;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.configuration.SpringDocConfiguration;
import org.springdoc.core.utils.Constants;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import static org.springdoc.core.utils.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConfigurationProperties(prefix = "springdoc.swagger-ui")
@ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
@ConditionalOnBean(SpringDocConfiguration.class)
public class SwaggerUiConfigProperties extends AbstractSwaggerUiConfigProperties {
private boolean disableSwaggerDefaultUrl;
private String version;
private Csrf csrf = new Csrf();
private SyntaxHighlight syntaxHighlight = new SyntaxHighlight();
private boolean enabled = true;
private boolean useRootPath;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public boolean isUseRootPath() {
return useRootPath;
}
public void setUseRootPath(boolean useRootPath) {
this.useRootPath = useRootPath;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isDisableSwaggerDefaultUrl() {
return disableSwaggerDefaultUrl;
}
public void setDisableSwaggerDefaultUrl(boolean disableSwaggerDefaultUrl) {
this.disableSwaggerDefaultUrl = disableSwaggerDefaultUrl;
}
public Csrf getCsrf() {
return csrf;
}
public void setCsrf(Csrf csrf) {
this.csrf = csrf;
}
public boolean isCsrfEnabled() {
return csrf.isEnabled();
}
public SyntaxHighlight getSyntaxHighlight() {
return syntaxHighlight;
}
public void setSyntaxHighlight(SyntaxHighlight syntaxHighlight) {
this.syntaxHighlight = syntaxHighlight;
}
public Set<SwaggerUrl> cloneUrls() {
return this.urls.stream().map(swaggerUrl -> new SwaggerUrl(swaggerUrl.getName(), swaggerUrl.getUrl(), swaggerUrl.getDisplayName())).collect(Collectors.toSet());
}
public static class Csrf {
private boolean enabled;
private boolean useLocalStorage;
private boolean useSessionStorage;
private String cookieName = Constants.CSRF_DEFAULT_COOKIE_NAME;
private String localStorageKey = Constants.CSRF_DEFAULT_LOCAL_STORAGE_KEY;
private String sessionStorageKey = Constants.CSRF_DEFAULT_LOCAL_STORAGE_KEY;
private String headerName = Constants.CSRF_DEFAULT_HEADER_NAME;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isUseLocalStorage() {
return useLocalStorage;
}
public void setUseLocalStorage(boolean useLocalStorage) {
this.useLocalStorage = useLocalStorage;
}
public boolean isUseSessionStorage() {
return useSessionStorage;
}
public void setUseSessionStorage(boolean useSessionStorage) {
this.useSessionStorage = useSessionStorage;
}
public String getCookieName() {
return cookieName;
}
public void setCookieName(String cookieName) {
this.cookieName = cookieName;
}
public String getLocalStorageKey() {
return localStorageKey;
}
public void setLocalStorageKey(String localStorageKey) {
this.localStorageKey = localStorageKey;
}
public String getSessionStorageKey() {
return sessionStorageKey;
}
public void setSessionStorageKey(String sessionStorageKey) {
this.sessionStorageKey = sessionStorageKey;
}
public String getHeaderName() {
return headerName;
}
public void setHeaderName(String headerName) {
this.headerName = headerName;
}
}
public static class SyntaxHighlight {
private Boolean activated;
private String theme;
public Boolean getActivated() {
return activated;
}
public void setActivated(Boolean activated) {
this.activated = activated;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public boolean isPresent() {
return activated != null || StringUtils.isNotEmpty(theme);
}
}
}