0
# Configuration
1
2
Setup and configuration capabilities for enabling Swagger 2 documentation generation in Spring applications. Provides simple annotations and auto-configuration for both WebMVC and WebFlux application types.
3
4
## Capabilities
5
6
### @EnableSwagger2WebMvc
7
8
Enables Swagger 2 support for Spring WebMVC applications. This annotation imports the necessary configuration for servlet-based Spring applications.
9
10
```java { .api }
11
/**
12
* Indicates that Swagger support should be enabled for WebMVC applications.
13
* Must be applied to a Spring configuration class with @Configuration annotation.
14
*/
15
@Retention(RetentionPolicy.RUNTIME)
16
@Target(ElementType.TYPE)
17
@Documented
18
@Import({Swagger2DocumentationWebMvcConfiguration.class})
19
@ConditionalOnWebApplication
20
public @interface EnableSwagger2WebMvc {
21
}
22
```
23
24
**Usage Example:**
25
```java
26
import org.springframework.context.annotation.Configuration;
27
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
28
29
@Configuration
30
@EnableSwagger2WebMvc
31
public class SwaggerConfig {
32
// Additional Docket beans can be defined here
33
}
34
```
35
36
### @EnableSwagger2WebFlux
37
38
Enables Swagger 2 support for Spring WebFlux reactive applications. This annotation imports the necessary configuration for reactive web applications.
39
40
```java { .api }
41
/**
42
* Indicates that Swagger support should be enabled for WebFlux applications.
43
* Must be applied to a Spring configuration class with @Configuration annotation.
44
*/
45
@Retention(RetentionPolicy.RUNTIME)
46
@Target(ElementType.TYPE)
47
@Documented
48
@Import({Swagger2DocumentationWebFluxConfiguration.class})
49
public @interface EnableSwagger2WebFlux {
50
}
51
```
52
53
**Usage Example:**
54
```java
55
import org.springframework.context.annotation.Configuration;
56
import springfox.documentation.swagger2.annotations.EnableSwagger2WebFlux;
57
58
@Configuration
59
@EnableSwagger2WebFlux
60
public class SwaggerConfig {
61
// Additional Docket beans can be defined here
62
}
63
```
64
65
### Swagger2DocumentationWebMvcConfiguration
66
67
Spring configuration class that provides the necessary beans for WebMVC Swagger 2 integration.
68
69
```java { .api }
70
/**
71
* Auto-configuration class for WebMVC Swagger 2 integration
72
*/
73
@Configuration
74
@ConditionalOnClass(name = "springfox.documentation.spring.web.SpringfoxWebMvcConfiguration")
75
@Import({ SpringfoxWebConfiguration.class, SpringfoxWebMvcConfiguration.class, SwaggerCommonConfiguration.class })
76
@ComponentScan(basePackages = {
77
"springfox.documentation.swagger2.readers.parameter",
78
"springfox.documentation.swagger2.mappers"
79
})
80
public class Swagger2DocumentationWebMvcConfiguration {
81
82
/**
83
* Provides Jackson module for Swagger 2 JSON serialization
84
*/
85
public JacksonModuleRegistrar swagger2Module();
86
87
/**
88
* Creates handler mapping for Swagger 2 controller
89
* @param environment Spring environment for property resolution
90
* @param documentationCache Cache for documentation access
91
* @param mapper Mapper for converting documentation to Swagger format
92
* @param jsonSerializer JSON serializer for Swagger objects
93
* @return Handler mapping for Swagger endpoints
94
*/
95
public HandlerMapping swagger2ControllerMapping(
96
Environment environment,
97
DocumentationCache documentationCache,
98
ServiceModelToSwagger2Mapper mapper,
99
JsonSerializer jsonSerializer
100
);
101
}
102
```
103
104
### Swagger2DocumentationWebFluxConfiguration
105
106
Spring configuration class that provides the necessary beans for WebFlux Swagger 2 integration.
107
108
```java { .api }
109
/**
110
* Auto-configuration class for WebFlux Swagger 2 integration
111
*/
112
@Configuration
113
@ConditionalOnClass(name = "org.springframework.web.reactive.BindingContext")
114
@Import({ SpringfoxWebConfiguration.class, SpringfoxWebFluxConfiguration.class, SwaggerCommonConfiguration.class,
115
Swagger2ControllerWebFlux.class })
116
@ComponentScan(basePackages = {
117
"springfox.documentation.swagger2.readers.parameter",
118
"springfox.documentation.swagger2.mappers"
119
})
120
public class Swagger2DocumentationWebFluxConfiguration {
121
122
/**
123
* Provides Jackson module for Swagger 2 JSON serialization
124
*/
125
public JacksonModuleRegistrar swagger2Module();
126
}
127
```
128
129
## Configuration Properties
130
131
The following properties can be used to customize Swagger 2 behavior:
132
133
- `springfox.documentation.swagger.v2.host` - Override the hostname in generated Swagger specification
134
- `springfox.documentation.swagger.v2.path` - Custom path for the API documentation endpoint (default: /v2/api-docs)
135
136
**Example application.properties:**
137
```properties
138
# Override hostname in Swagger spec
139
springfox.documentation.swagger.v2.host=api.example.com
140
141
# Custom API docs endpoint path
142
springfox.documentation.swagger.v2.path=/api/swagger.json
143
```
144
145
## Component Scanning
146
147
Both configuration classes automatically scan for components in these packages:
148
- `springfox.documentation.swagger2.readers.parameter` - Parameter readers and processors
149
- `springfox.documentation.swagger2.mappers` - Object mappers for Swagger 2 conversion
150
151
## Dependencies
152
153
These configuration classes require the following dependencies to be on the classpath:
154
- Spring Boot Autoconfigure
155
- Springfox core libraries (springfox-spring-web, springfox-swagger-common)
156
- Jackson for JSON processing
157
- Swagger 2 models (io.swagger.models)
158
159
**WebMVC specific:**
160
- Spring Web MVC framework
161
- Servlet API
162
163
**WebFlux specific:**
164
- Spring WebFlux framework
165
- Reactive streams API