在Linux环境中使用Swagger(现更名为OpenAPI Specification)时,遵循最佳实践可以帮助你更高效地使用该工具,并确保系统的安全性和可维护性。以下是一些关键的最佳实践:
sudo apt update
sudo apt install openjdk-11-jdk maven
springdoc-openapi-starter-webmvc-ui
依赖,例如:<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
@Configuration
@OpenAPIDefinition(info = @Info(title = "我的API", version = "1.0"))
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
}
@Api
注解标记控制器类,@ApiParam
注解标记简单参数,@ApiModel
和@ApiModelProperty
注解标记对象参数,并对返回值进行相应注解。@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
通过遵循以上最佳实践,可以在Linux环境下高效、安全地使用Swagger,提升API文档质量,并优化整体系统性能。