spring boot快速搭建swagger

简介:

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
也就是说只要我们配置好swagger,swagger就会自动帮我们生成api文档,同时我们也可以对接口进行测试,省去了后端开发人员对接口的编写,方便了接口的测试。

添加依赖:

		<!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version> 2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version> 2.7.0</version>
        </dependency>

编写配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("题目")
                .description("接口文档描述")
                .version("1.0")
                .contact(new Contact("zyw", "http://zyw.com", "1123@qq.com"))
                .build();
    }
}

为了增加api文档的可读性,可以在controller上加注解,从而给文档加描述声明:

@Api(description = "账户管理") //加在类上
@ApiOperation("查询所有账户")	//加在方法上
@ApiParam(value = "账户id",required = true)		//加在属性上

例如:
在这里插入图片描述
在这里插入图片描述
启动spring boot项目 访问:

http://127.0.0.1:8080/swagger-ui.html

在这里插入图片描述
在这里插入图片描述
可以直接在这里测试,省去了api开发文档的编写,也方便了接口测试。

帮助到您请点赞关注收藏谢谢!!!

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值