Swagger 2 API documentation integration library for Spring WebMVC and WebFlux applications
—
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.
Enables Swagger 2 support for Spring WebMVC applications. This annotation imports the necessary configuration for servlet-based Spring applications.
/**
* Indicates that Swagger support should be enabled for WebMVC applications.
* Must be applied to a Spring configuration class with @Configuration annotation.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import({Swagger2DocumentationWebMvcConfiguration.class})
@ConditionalOnWebApplication
public @interface EnableSwagger2WebMvc {
}Usage Example:
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
// Additional Docket beans can be defined here
}Enables Swagger 2 support for Spring WebFlux reactive applications. This annotation imports the necessary configuration for reactive web applications.
/**
* Indicates that Swagger support should be enabled for WebFlux applications.
* Must be applied to a Spring configuration class with @Configuration annotation.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import({Swagger2DocumentationWebFluxConfiguration.class})
public @interface EnableSwagger2WebFlux {
}Usage Example:
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebFlux;
@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig {
// Additional Docket beans can be defined here
}Spring configuration class that provides the necessary beans for WebMVC Swagger 2 integration.
/**
* Auto-configuration class for WebMVC Swagger 2 integration
*/
@Configuration
@ConditionalOnClass(name = "springfox.documentation.spring.web.SpringfoxWebMvcConfiguration")
@Import({ SpringfoxWebConfiguration.class, SpringfoxWebMvcConfiguration.class, SwaggerCommonConfiguration.class })
@ComponentScan(basePackages = {
"springfox.documentation.swagger2.readers.parameter",
"springfox.documentation.swagger2.mappers"
})
public class Swagger2DocumentationWebMvcConfiguration {
/**
* Provides Jackson module for Swagger 2 JSON serialization
*/
public JacksonModuleRegistrar swagger2Module();
/**
* Creates handler mapping for Swagger 2 controller
* @param environment Spring environment for property resolution
* @param documentationCache Cache for documentation access
* @param mapper Mapper for converting documentation to Swagger format
* @param jsonSerializer JSON serializer for Swagger objects
* @return Handler mapping for Swagger endpoints
*/
public HandlerMapping swagger2ControllerMapping(
Environment environment,
DocumentationCache documentationCache,
ServiceModelToSwagger2Mapper mapper,
JsonSerializer jsonSerializer
);
}Spring configuration class that provides the necessary beans for WebFlux Swagger 2 integration.
/**
* Auto-configuration class for WebFlux Swagger 2 integration
*/
@Configuration
@ConditionalOnClass(name = "org.springframework.web.reactive.BindingContext")
@Import({ SpringfoxWebConfiguration.class, SpringfoxWebFluxConfiguration.class, SwaggerCommonConfiguration.class,
Swagger2ControllerWebFlux.class })
@ComponentScan(basePackages = {
"springfox.documentation.swagger2.readers.parameter",
"springfox.documentation.swagger2.mappers"
})
public class Swagger2DocumentationWebFluxConfiguration {
/**
* Provides Jackson module for Swagger 2 JSON serialization
*/
public JacksonModuleRegistrar swagger2Module();
}The following properties can be used to customize Swagger 2 behavior:
springfox.documentation.swagger.v2.host - Override the hostname in generated Swagger specificationspringfox.documentation.swagger.v2.path - Custom path for the API documentation endpoint (default: /v2/api-docs)Example application.properties:
# Override hostname in Swagger spec
springfox.documentation.swagger.v2.host=api.example.com
# Custom API docs endpoint path
springfox.documentation.swagger.v2.path=/api/swagger.jsonBoth configuration classes automatically scan for components in these packages:
springfox.documentation.swagger2.readers.parameter - Parameter readers and processorsspringfox.documentation.swagger2.mappers - Object mappers for Swagger 2 conversionThese configuration classes require the following dependencies to be on the classpath:
WebMVC specific:
WebFlux specific:
Install with Tessl CLI
npx tessl i tessl/maven-io-springfox--springfox-swagger2