Springboot项目中Swagger的使用
Maven依赖
<properties>
<swagger.version>2.7.0</swagger.version>
</properties>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
配置类SwaggerConfig
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//环境一
@Bean
public Docket docket1(Environment environment)
{
Profiles profiles = Profiles.of("test","dev"); //profile active是dev和test
boolean flag = environment.acceptsProfiles(profiles); //查询环境是否是上述两种
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag) //为flag为true是开启
.groupName("一组开发")
.select()
//记得修改包名,报路径和开发的controller保持一致,或者在它的上一级
.apis(RequestHandlerSelectors.basePackage("com.team1.controller"))
.build();
}
//环境二
@Bean
public Docket docket(Environment environment)
{
Profiles profiles = Profiles.of("test","dev");
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)
.groupName("二组开发")
.select()
//记得修改包名,
.apis(RequestHandlerSelectors.basePackage("com.team2.controller"))
.build();
}
//这里的参数是页面上的显示
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger接口列表")
.description("接口")
.termsOfServiceUrl("http://localhost:8081/swagger-ui.html")
.contact(new Contact("SWAGGER", "http://www.baidu.com", "11111111@qq.com"))
.version("1.1.0")
.build();
}
}
简单使用
- 在controller接口方法上加
@ApiOperation(" 说明 ")
起到注释说明的作用 - 在实体类上加
@ApiModel(value=" 说明 ", description=" 描述 ")
- 在实体列属性上加
@ApiModelProperty(value = " 说明 ")
- 通过
http://loaclhost:<port>/swagger-ui.html
即可访问,进行接口测试
了解更多可到官网进行学习:https://swagger.io