1
18
19 package org.springdoc.webmvc.ui;
20
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import org.springdoc.core.SpringDocConfigProperties;
23 import org.springdoc.core.SpringDocConfiguration;
24 import org.springdoc.core.SwaggerUiConfigProperties;
25 import org.springdoc.core.SwaggerUiOAuthProperties;
26
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
30 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.Configuration;
33 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
34 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
35
36 import static org.springdoc.core.Constants.CLASSPATH_RESOURCE_LOCATION;
37 import static org.springdoc.core.Constants.DEFAULT_WEB_JARS_PREFIX_URL;
38 import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;
39 import static org.springdoc.core.Constants.SWAGGER_UI_PATH;
40 import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
41
42
43 @Configuration
44 @ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
45 @ConditionalOnBean(SpringDocConfiguration.class)
46 @SuppressWarnings("deprecation")
47 public class SwaggerConfig extends WebMvcConfigurerAdapter {
48
49 @Value(SWAGGER_UI_PATH)
50 private String swaggerPath;
51
52 @Autowired
53 private SwaggerIndexTransformer swaggerIndexTransformer;
54
55 @Override
56 public void addResourceHandlers(ResourceHandlerRegistry registry) {
57 StringBuilder uiRootPath = new StringBuilder();
58 if (swaggerPath.contains("/"))
59 uiRootPath.append(swaggerPath, 0, swaggerPath.lastIndexOf('/'));
60 uiRootPath.append("/**");
61 registry.addResourceHandler(uiRootPath + "/swagger-ui/**")
62 .addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR)
63 .resourceChain(false)
64 .addTransformer(swaggerIndexTransformer);
65 }
66
67 @Bean
68 @ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
69 SwaggerWelcome swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties) {
70 return new SwaggerWelcome(swaggerUiConfig, springDocConfigProperties);
71 }
72
73 @Bean
74 @ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
75 SwaggerIndexTransformer indexPageTransformer(SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
76 return new SwaggerIndexTransformer(swaggerUiOAuthProperties, objectMapper);
77 }
78 }
79