- Spring 5.0 By Example
- Claudio Eduardo de Oliveira
- 194字
- 2021-06-24 19:17:37
Configuring Swagger
The dependencies are added, now we can configure the infrastructure for Swagger. The configuration is pretty simple. We will create a class with @Configuration to produce the Swagger configuration for the Spring container. Let's do it.
Take a look at the following Swagger configuration:
package springfive.cms.infra.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket documentation() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build();
}
}
The @Configuration instructs the Spring to generate a bean definition for Swagger. The annotation, @EnableSwagger2 adds support for Swagger. @EnableSwagger2 should be accompanied by @Configuration, it is mandatory.
The Docket class is a builder to create an API definition, and it provides sensible defaults and convenience methods for configuration of the Spring Swagger MVC Framework.
The invocation of method .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) instructs the framework to handle classes annotated with @RestController.
There are many methods to customize the API documentation, for example, there is a method to add authentication headers.
That is the Swagger configuration, in the next section, we will create a first documented API.
- R語言經(jīng)典實例(原書第2版)
- HTML5 and CSS3 Transition,Transformation,and Animation
- The DevOps 2.4 Toolkit
- MySQL數(shù)據(jù)庫基礎實例教程(微課版)
- HTML5+CSS3網(wǎng)站設計基礎教程
- Visual C++從入門到精通(第2版)
- Redmine Cookbook
- TypeScript全棧開發(fā)
- Laravel Design Patterns and Best Practices
- Implementing Domain:Specific Languages with Xtext and Xtend
- C語言程序設計實驗指導
- 劍指大數(shù)據(jù):企業(yè)級電商數(shù)據(jù)倉庫項目實戰(zhàn)(精華版)
- Learning Gerrit Code Review
- HTML+CSS+JavaScript前端開發(fā)(慕課版)
- Kudu:構建高性能實時數(shù)據(jù)分析存儲系統(tǒng)