Swagger 2 API documentation integration library for Spring WebMVC and WebFlux applications
npx @tessl/cli install tessl/maven-io-springfox--springfox-swagger2@2.10.0Springfox Swagger2 provides comprehensive Swagger 2 API documentation integration for Spring WebMVC and WebFlux applications. It automatically generates OpenAPI specifications from Spring controllers and models, offering seamless integration with Spring Boot through autoconfiguration.
Maven:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.10.5</version>
</dependency>Gradle:
implementation 'io.springfox:springfox-swagger2:2.10.5'import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebFlux;WebMVC Integration:
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
// Configuration class automatically sets up Swagger 2 documentation
}WebFlux Integration:
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebFlux;
@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig {
// Configuration class automatically sets up Swagger 2 documentation
}Springfox Swagger2 is built around several key components:
Easy activation of Swagger 2 documentation generation through Spring configuration annotations. Supports both WebMVC and WebFlux application types.
@EnableSwagger2WebMvc
public @interface EnableSwagger2WebMvc;
@EnableSwagger2WebFlux
public @interface EnableSwagger2WebFlux;Core functionality that converts Spring application documentation into Swagger 2 specification format through comprehensive mapping capabilities.
public abstract class ServiceModelToSwagger2Mapper {
public abstract Swagger mapDocumentation(Documentation from);
}REST endpoints and controllers that serve generated Swagger 2 JSON documentation to clients and Swagger UI applications.
public class Swagger2ControllerWebMvc {
public ResponseEntity<Json> getDocumentation(
@RequestParam(value = "group", required = false) String swaggerGroup,
HttpServletRequest servletRequest
);
}
public class Swagger2ControllerWebFlux {
public ResponseEntity<Json> getDocumentation(
@RequestParam(value = "group", required = false) String swaggerGroup,
ServerHttpRequest request
);
}Customized Jackson module for proper Swagger 2 JSON serialization with support for vendor extensions and example values.
public class Swagger2JacksonModule extends SimpleModule implements JacksonModuleRegistrar {
public void maybeRegisterModule(ObjectMapper objectMapper);
public void setupModule(SetupContext context);
}springfox.documentation.swagger.v2.host - Override hostname in generated Swagger specificationspringfox.documentation.swagger.v2.path - Custom path for API documentation endpoint (default: /v2/api-docs)// Core Springfox types used throughout the API
interface Documentation {
// Springfox documentation model
}
interface DocumentationCache {
Documentation documentationByGroup(String groupName);
}
interface JsonSerializer {
Json toJson(Object object);
}
// Swagger 2 model types
class Swagger {
String getHost();
String getBasePath();
void host(String host);
void basePath(String basePath);
}
class Json {
// JSON wrapper for serialized Swagger documentation
}