Springboot项目中Swagger的使用

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();
    }
}

简单使用

  1. 在controller接口方法上加 @ApiOperation(" 说明 ")起到注释说明的作用
  2. 在实体类上加@ApiModel(value=" 说明 ", description=" 描述 ")
  3. 在实体列属性上加@ApiModelProperty(value = " 说明 ")
  4. 通过http://loaclhost:<port>/swagger-ui.html 即可访问,进行接口测试

了解更多可到官网进行学习:https://swagger.io

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值