Swagger 2 API documentation integration library for Spring WebMVC and WebFlux applications
—
REST endpoints and controllers that serve generated Swagger 2 JSON documentation to clients and Swagger UI applications. Provides both WebMVC and WebFlux controller implementations for different Spring application types.
Spring WebMVC controller that serves Swagger 2 JSON documentation via HTTP endpoints in servlet-based applications.
/**
* Controller for serving Swagger 2 documentation in WebMVC applications
* Handles HTTP requests and returns JSON-formatted Swagger specifications
*/
@Controller
@ConditionalOnClass(name = "javax.servlet.http.HttpServletRequest")
@ApiIgnore
public class Swagger2ControllerWebMvc {
/**
* Constructor for WebMVC Swagger controller
* @param environment Spring environment for property resolution
* @param documentationCache Cache for accessing documentation by group
* @param mapper Mapper for converting documentation to Swagger format
* @param jsonSerializer Serializer for converting Swagger objects to JSON
*/
public Swagger2ControllerWebMvc(
Environment environment,
DocumentationCache documentationCache,
ServiceModelToSwagger2Mapper mapper,
JsonSerializer jsonSerializer
);
/**
* GET endpoint that returns Swagger 2 JSON documentation
* Default endpoint: /v2/api-docs
* Configurable via: springfox.documentation.swagger.v2.path
*
* @param swaggerGroup Optional group name for multi-group documentation (default: "default")
* @param servletRequest HTTP servlet request for host/path resolution
* @return ResponseEntity containing JSON-formatted Swagger specification
*/
@RequestMapping(
value = "/v2/api-docs",
method = RequestMethod.GET,
produces = { "application/json", "application/hal+json" }
)
@PropertySourcedMapping(
value = "${springfox.documentation.swagger.v2.path}",
propertyKey = "springfox.documentation.swagger.v2.path"
)
@ResponseBody
public ResponseEntity<Json> getDocumentation(
@RequestParam(value = "group", required = false) String swaggerGroup,
HttpServletRequest servletRequest
);
}Usage Example:
# Get default group documentation
curl http://localhost:8080/v2/api-docs
# Get specific group documentation
curl http://localhost:8080/v2/api-docs?group=admin-api
# Custom endpoint path (if configured)
curl http://localhost:8080/api/swagger.jsonSpring WebFlux controller that serves Swagger 2 JSON documentation via HTTP endpoints in reactive applications.
/**
* Controller for serving Swagger 2 documentation in WebFlux applications
* Handles reactive HTTP requests and returns JSON-formatted Swagger specifications
*/
@Controller
@ConditionalOnClass(name = "org.springframework.web.reactive.BindingContext")
@ApiIgnore
public class Swagger2ControllerWebFlux {
/**
* Constructor for WebFlux Swagger controller
* @param documentationCache Cache for accessing documentation by group
* @param mapper Mapper for converting documentation to Swagger format
* @param jsonSerializer Serializer for converting Swagger objects to JSON
*/
public Swagger2ControllerWebFlux(
DocumentationCache documentationCache,
ServiceModelToSwagger2Mapper mapper,
JsonSerializer jsonSerializer
);
/**
* GET endpoint that returns Swagger 2 JSON documentation
* Default endpoint: /v2/api-docs
* Configurable via: springfox.documentation.swagger.v2.path
*
* @param swaggerGroup Optional group name for multi-group documentation (default: "default")
* @param request Reactive HTTP request for host/path resolution
* @return ResponseEntity containing JSON-formatted Swagger specification
*/
@RequestMapping(
value = "/v2/api-docs",
method = RequestMethod.GET,
produces = { "application/json", "application/hal+json" }
)
@PropertySourcedMapping(
value = "${springfox.documentation.swagger.v2.path}",
propertyKey = "springfox.documentation.swagger.v2.path"
)
@ResponseBody
public ResponseEntity<Json> getDocumentation(
@RequestParam(value = "group", required = false) String swaggerGroup,
ServerHttpRequest request
);
}Usage Example:
# Get default group documentation (same as WebMVC)
curl http://localhost:8080/v2/api-docs
# Get specific group documentation
curl http://localhost:8080/v2/api-docs?group=admin-apiBoth controllers serve documentation at /v2/api-docs by default, which is the standard Swagger 2 documentation endpoint expected by Swagger UI and other tools.
You can customize the endpoint path using the springfox.documentation.swagger.v2.path property:
# application.properties
springfox.documentation.swagger.v2.path=/api/swagger.jsonWebMVC controller supports hostname override via the springfox.documentation.swagger.v2.host property:
# application.properties
springfox.documentation.swagger.v2.host=api.example.comBoth controllers support multiple content types:
application/json - Standard JSON formatapplication/hal+json - HAL (Hypertext Application Language) JSON formatBoth controllers support documentation groups, allowing you to serve different API documentation for different parts of your application:
// Multiple Docket beans create different groups
@Bean
public Docket publicApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("public")
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.public"))
.build();
}
@Bean
public Docket adminApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("admin")
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.admin"))
.build();
}Access different groups:
/v2/api-docs - Default group/v2/api-docs?group=public - Public API group/v2/api-docs?group=admin - Admin API groupHttpServletRequest for accessing request informationServerHttpRequest for accessing request informationBoth controllers return ResponseEntity<Json> where:
Example success response structure:
{
"swagger": "2.0",
"info": {
"title": "API Documentation",
"version": "1.0.0"
},
"host": "localhost:8080",
"basePath": "/",
"paths": {
"/api/users": {
"get": {
"summary": "Get all users",
"responses": {
"200": {
"description": "Successful operation"
}
}
}
}
}
}These endpoints are designed to work seamlessly with Swagger UI:
// Swagger UI configuration
SwaggerUIBundle({
url: 'http://localhost:8080/v2/api-docs',
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
]
});Both controllers handle common error scenarios:
Controllers are conditionally loaded based on classpath:
This ensures the appropriate controller is used based on your application type.
Install with Tessl CLI
npx tessl i tessl/maven-io-springfox--springfox-swagger2